Jump to content

preg_replace


dreamwest

Recommended Posts

Im trying to highlight some words within a html string but its returning highlights between tags.

 

$txt = "Hello this is a <a href='/link.php' title='hello php'>link</a> within php";
echo preg_replace("/\b(hello|php)\b/is",'<b style="color:black;background-color: #FFFF00">\1</b>',$txt);

 

I need it to return:

<b style="color:black;background-color: #FFFF00">Hello</b> this is a <a href='/link.php' title='hello php'>link</a> within <b style="color:black;background-color: #FFFF00">php</b>

Link to comment
https://forums.phpfreaks.com/topic/242704-preg_replace/
Share on other sites

This is really advanced but say thank you to me for designing this right-on-the-spot regex:

 

$txt = 'Hello this is a <a href="/link.php" title="hello php">link</a> within php';
echo preg_replace('#(hello|php)(?!([^<]+)?>)#is', '<b style="color:black;background-color: #FFFF00">$1</b>', $txt);

 

 

Link to comment
https://forums.phpfreaks.com/topic/242704-preg_replace/#findComment-1246643
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.