dreamwest Posted July 25, 2011 Share Posted July 25, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/242704-preg_replace/ Share on other sites More sharing options...
silkfire Posted July 25, 2011 Share Posted July 25, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/242704-preg_replace/#findComment-1246643 Share on other sites More sharing options...
dreamwest Posted July 25, 2011 Author Share Posted July 25, 2011 Awesome! Works perfect ...i love you long time Quote Link to comment https://forums.phpfreaks.com/topic/242704-preg_replace/#findComment-1246722 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.