hilight substring

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

hilight substring

Post by yaron »

Hello all,
I have this code

Code: Select all

<?php
$string='<This is my string>';
$sub="is";
$hilighted=str_replace($sub,"<font color=yellow>$sub</font>",$string);
echo htmlentities($hilighted);
?>
Because my string is between <> I can't seem to hilight the substring because I need to use htmlentities!
I need to show the <> and to prevent the browser to process it like html tag!

any ideas?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Move the htmlentities() to the beginning.

Code: Select all

<?php
$string = htmlentities('<This is my string>');
$sub="is";
$hilighted=str_replace($sub,"<font color=yellow>$sub</font>",$string);
echo $hilighted;
?>
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

Post by yaron »

thanks,
silly me
So simple ;-)
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

or you could use < and >
Post Reply