Ryan0r Posted March 6, 2009 Share Posted March 6, 2009 <?php $arrayofwords = array (); $arrayofwords[0] = "This"; $arrayofwords[1] = "text"; $arrayofwords[2] = "need"; $arrayofwords[3] = "words"; $str = 'This is my <img src="" title="This image text"> long text <a href="#">words</a> where I need to highlight words in the HTML text.'; $str = preg_replace ( "/(?!(?:[^<]+>|[^>]+<\/a>))\b(" . implode ( '|', $arrayofwords ) . ")\b/is", "<strong>\\1</strong>", $str ); echo $str; ?> This example here is not fully working, how do I change the Regex so that it replaces the text that is wrapped by the <a> tag? Eg: <a href="#">words</a> is not becoming <a href="#"><strong>words</strong></a> when I need it to. Any ideas how to fix this? Cheers! Ryan Quote Link to comment https://forums.phpfreaks.com/topic/148173-regex-ignore-html-replace-text/ Share on other sites More sharing options...
sasa Posted March 6, 2009 Share Posted March 6, 2009 try <?php $arrayofwords = array (); $arrayofwords[0] = "This"; $arrayofwords[1] = "text"; $arrayofwords[2] = "need"; $arrayofwords[3] = "words"; $str = 'This is my <img src="" title="This image text"> long text <a href="#">words</a> where I need to highlight words in the HTML text.'; $str = preg_replace ( "/(?!(?:[^<]+>))\b(" . implode ( '|', $arrayofwords ) . ")\b/is", "<strong>\\1</strong>", $str ); echo $str; ?>f/code] Quote Link to comment https://forums.phpfreaks.com/topic/148173-regex-ignore-html-replace-text/#findComment-777880 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.