Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

substitution with capitals

Status
Not open for further replies.

forces1

Programmer
Apr 5, 2007
29
NL
Hi all,

I have a searchfunction in perl on my website, and when visitors use this function, it will use the file linkbuild1.pl. Now, I have this code on the file linkbuild1.pl:

Code:
$description = $linkinfo[7];
foreach $line (@searchterms) {
$description =~ s/$line/<b>$line<\/b>/ig;
}

It replaces all searchterms (that is the $line) in the description for the same searchterm, but then bolded.
The problem is that this code doesn't seem to reconize any capitals. For example: it replaces Amsterdam for <b>amsterdam</b> in stead of <b>Amsterdam</b>. I know that it has anything to do with the i at the end of the code, but when I remove the i, it will only replace amsterdam and leaves Amsterdam untouched.

So now my question is: how can I make this line replace amsterdam for <b>amsterdam</b> and Amsterdam for <b>Amsterdam</b>.

Thanks for all you help!
 
Try
Code:
$description =~ s/($line)/<b>$1<\/b>/ig;
This 'replaces' whatever it finds with itself wrapped in <b> tags. This will find things in the middle of words, too, which might not be what you want.
Code:
$description =~ s/\b($line)\b/<b>$1<\/b>/ig;
might be more selective (untested).

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Absolutely wonderfull, your first suggestion works! Thank you very much! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top