Gossamer Forum
Home : General : Perl Programming :

Change part of a word to uppercase and bold? how?

Quote Reply
Change part of a word to uppercase and bold? how?
i've written a search script with a mysql database and perl and it works sweet, it highlights the word i search for in capitals and makes it bold.
The problem i have is if the search word is half of a word it wont make it bold, like if i search for the word 'man', it will return strings with man in them like 'manager', 'mandrake' etc..., what i want to do is make the 'man' part of those found words bolded and leave the rest of the word normal like this 'MANager', 'MANdrake' etc.., can anyone tell me the code to enable me to do this? at the moment, it dont do it, but if it finds the work 'man' by itseslf, it will turn it bold no worries, its only when its part of another work it wont make it bold.

later...

Last edited by:

TheIceman: Oct 14, 2002, 5:18 AM
Quote Reply
Re: [TheIceman] Change part of a word to uppercase and bold? how? In reply to
What code do you have so far?
Quote Reply
Re: [Paul] Change part of a word to uppercase and bold? how? In reply to
cant remember it fully, its not here, but it goes something like this.

$string =~ s/$search_word/<b>$search_word<\/b>/g

it works, but it wont work when the search word is part of another word like above in my first post.

later...
Quote Reply
Re: [TheIceman] Change part of a word to uppercase and bold? how? In reply to
Try this:

Code:
$string =~ s|(\Q$search_word\E)|"<b>\U$1\E</b>"|ige;

If $string was "How now brown cow" and $search_word was "brown" the output would be:

Code:
How now BROWN cow

Last edited by:

Paul: Oct 14, 2002, 6:19 AM
Quote Reply
Re: [Paul] Change part of a word to uppercase and bold? how? In reply to
Cool thanks, that worked great. I must do a bit more learning on perl so i can figure this stuff out by myself.
Can u explain how that piece of code works? i cant work it out. thanks.

later...

Last edited by:

TheIceman: Oct 14, 2002, 6:29 AM
Quote Reply
Re: [TheIceman] Change part of a word to uppercase and bold? how? In reply to
(\Q$search_word\E)

That code looks for $search_word in $string, it also quotes any meta-characters that may appear in the word so that they are treated as literal and not meta-characters - that is the \Q \E part...\Q is quote and \E tells the regex engine to end quoting....meta-characters are like * + etc.

The brackets () around the word assign the matched value to $1

"<b>\U$1\E</b>"

This bit replaces the matched value with itself except the bold html tags bold it and the \U \E uppercase it. \U is for uppercase and like with the quoting \E stops the uppercasing.

The i is for case insensitivity
The g is for global
The e is for evaluate

The e evaluates the replacement as perl code which is why it is quoted with " " ....you would get an error without the quoting.
Quote Reply
Re: [Paul] Change part of a word to uppercase and bold? how? In reply to
ok sweet, thats clearing it up a bit, what does the | do in there? is that just seperating the stuff?
Quote Reply
Re: [TheIceman] Change part of a word to uppercase and bold? how? In reply to
Yeah | does the same as / except you can use </b> instead of <\/b>
Quote Reply
Re: [Paul] Change part of a word to uppercase and bold? how? In reply to
cool thanks. think i got it now.

later...