The Little Guy Posted October 3, 2011 Share Posted October 3, 2011 $html = '<?xml version="1.0" encoding="UTF-8"?> <root> <!-- People who are 25 years old --> <names age="25"> <name>Ryan</name> <name>Bill</name> <name>Bob</name> </names> <!-- People who are 45 years old --> <names age="45"> <name>Roy</name> <name>Jill</name> <name>Jimmy</name> </names> </root>'; $com = '#34803a'; echo preg_replace('~(<\!--.*?-->)~s', '<span style="font-style:italic;color:'.$com.';">$1</span>', $html); I have the above regexp and it works for these lines: <!-- People who are 25 years old --> but it doesn't work for this one: <!-- People who are 45 years old --> What can I do to fix this so it works for both? Visual Example: http://tests.phpsnips.com/phpLive/examples/misc/highlight.php Quote Link to comment Share on other sites More sharing options...
requinix Posted October 3, 2011 Share Posted October 3, 2011 It's working just fine. Notice how the comment is in italics? <!-- People who are 45 years old --> The problem is the other span overriding it. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted October 3, 2011 Author Share Posted October 3, 2011 Then it must be one of the others, and I think that it is the 4th one messing up the comments... $tag = '#0000ff'; $att = '#ff0000'; $val = '#8000ff'; $com = '#34803a'; $find = array( '~(\s[a-z].*?=)~', // Highlight the attributes '~(<\!--.*?-->)~s', // Hightlight comments '~("[a-zA-Z0-9\/].*?")~', // Highlight the values '~(<[a-zA-Z!?].*?>)~', // Highlight the beginning of the opening tag '~(</[a-zA-Z].*?>)~', // Highlight the closing tag '~(&.*?~', // Stylize HTML entities ); $replace = array( '<span style="color:'.$att.';">$1</span>', '<span style="font-style:italic;color:'.$com.';">$1</span>', '<span style="color:'.$val.';">$1</span>', '<span style="color:'.$tag.';">$1</span>', '<span style="color:'.$tag.';">$1</span>', '<span style="font-style:italic;">$1</span>', ); Quote Link to comment Share on other sites More sharing options...
requinix Posted October 3, 2011 Share Posted October 3, 2011 '~(<[a-zA-Z!?].*?>)~', // Highlight the beginning of the opening tag Fix that one so it doesn't include comments. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted October 3, 2011 Author Share Posted October 3, 2011 what about doctype? <!DOCTYPE Quote Link to comment Share on other sites More sharing options...
requinix Posted October 3, 2011 Share Posted October 3, 2011 I didn't say "remove the exclamation mark" I like '~(<([a-z?]|!DOCTYPE).*?>)~i' Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted October 3, 2011 Author Share Posted October 3, 2011 Wonderful! Any other suggestions on my regexp? Oh, and another thing, in the first comment on the page, it highlights tags within comments. Any suggestions to stop that from happening? Thanks for your help! Quote Link to comment Share on other sites More sharing options...
requinix Posted October 3, 2011 Share Posted October 3, 2011 Comments should be handled separately from everything else. The strategy I use for that, which more commonly occurs with BBCode, is to preg_split using comments as delimiters. Alternating, one will be regular XML and the next will be a comment. $iscomment = false; // starts off outside comments foreach (preg_split('//s', $xml, -1, PREG_SPLIT_DELIM_CAPTURE) as $split) { if ($iscomment) { // just a comment } else { // not a comment. format } $iscomment = !$iscomment; } Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted October 4, 2011 Author Share Posted October 4, 2011 That seems to work! Result: http://phpsnips.com/phplive/examples/misc/highlight.php Thanks for the help!!! Quote Link to comment 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.