The Little Guy Posted March 6, 2009 Share Posted March 6, 2009 I am trying to highlight attributes in a html tag, for some reason, this just isn't working, does anyone know why? $string = ' <a href="home.html" style="color:blue;">Home</a> <p>Hello</p> '; $att = '#ff0000'; preg_replace('~^\s([a-z].*?)\=~', '<span style="color:'.$att.'">$1</span>', $string); Quote Link to comment https://forums.phpfreaks.com/topic/148191-highlight-attributes/ Share on other sites More sharing options...
nrg_alpha Posted March 6, 2009 Share Posted March 6, 2009 Sort of like this? $string = <<<DATA <a href="home.html" style="color:blue;">Home</a> <p>Hello</p> DATA; $att = '#ff0000'; $string = preg_replace('#(style=[\'"]color:)[^;]+(;[\'"])#', '$1'.$att.'$2', $string); echo $string; Quote Link to comment https://forums.phpfreaks.com/topic/148191-highlight-attributes/#findComment-777902 Share on other sites More sharing options...
The Little Guy Posted March 6, 2009 Author Share Posted March 6, 2009 Got it! http://phpsnips.com/snippet.php?id=70 '~(<\!--.*?-->)~', // Hightlight comments How can I make the above search multi line? Let me know what I can improve thanks! Quote Link to comment https://forums.phpfreaks.com/topic/148191-highlight-attributes/#findComment-777904 Share on other sites More sharing options...
nrg_alpha Posted March 6, 2009 Share Posted March 6, 2009 For multiple lines, you can add the 's' modifier after the closing delimiter in your pattern. Quote Link to comment https://forums.phpfreaks.com/topic/148191-highlight-attributes/#findComment-777906 Share on other sites More sharing options...
nrg_alpha Posted March 6, 2009 Share Posted March 6, 2009 You can read more about modifiers here. Quote Link to comment https://forums.phpfreaks.com/topic/148191-highlight-attributes/#findComment-777907 Share on other sites More sharing options...
The Little Guy Posted March 6, 2009 Author Share Posted March 6, 2009 kool thanks! I was trying to use the 'm' modifier! Quote Link to comment https://forums.phpfreaks.com/topic/148191-highlight-attributes/#findComment-777910 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.