Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
| Search | Today's Posts | Mark Forums Read
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 15th, 2004, 09:45 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default More regex help

Hello,

I need a regex that will take a block of text and highlight HTML syntax.

Text will be one color, tags will have another and attribute values will have another.

I don't need to highlight the text, and these are the two lines I use to highlight now:

Code:
    $text = preg_replace("/&lt;(.*)&gt;/","<span style='color:#000099;font-family:monospace;background-color:#CCCCCC'>&lt;\\1&gt;</span>",$text);
    $text = preg_replace("/&lt;(.*)(&quot;|\')(.*)(&quot;|\')(.*)&gt;/","&lt;\\1<span style='color:#0000FF;font-family:monospace;background-color:#CCCCCC;'>\\2\\3\\4</span>\\5&gt;",$text);
However they're very buggy. It only highlights the last attribute value of each line, and it highlights all the text in between tags as if they were tags as well.

Any ideas to improve these?

-Snib - http://www.snibworks.com
Where will you be in 100 years?
__________________
-Snib - http://www.snibworks.com
Where will you be in 100 years?
 
Old December 15th, 2004, 10:01 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Okay, I got the tag problem fixed. Now all I need to fix is the attribute value problem.

-Snib - http://www.snibworks.com
Where will you be in 100 years?
 
Old December 15th, 2004, 10:21 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Well, it seems that with a bit more thinking, I got the problem solved.

This is the solution I ended up using:

Code:
    $text = str_replace("&lt;","<span style='color:#000099;font-family:monospace;background-color:#CCCCCC'>&lt;",$text);
    $text = str_replace("&gt;","&gt;</span>",$text);
    $text = str_replace("=&quot;","=<span style='color:#0000FF;font-family:monospace;background-color:#CCCCCC'>&quot;",$text);
    $text = str_replace(" &quot;"," <span style='color:#0000FF;font-family:monospace;background-color:#CCCCCC'>&quot;",$text);
    $text = str_replace("&quot; ","&quot;</span> ",$text);
    $text = str_replace("&quot;&gt;","&quot;</span>&gt;",$text);
    $text = str_replace("&quot;/","&quot;</span>/",$text);
Thanks anyway,

-Snib - http://www.snibworks.com
Where will you be in 100 years?
 
Old December 16th, 2004, 05:35 AM
Authorized User
 
Join Date: Oct 2004
Location: Edinburgh, Scotland, United Kingdom.
Posts: 84
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to SiliconFuRy
Default

It's amazing what you can accomplish with a little bit of thought. And 2 Litres of coffee. And 10 packs of cigarettes. And 72 hours without sleep...
 
Old December 16th, 2004, 09:56 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Amen. LOL. :-)

-Snib - http://www.snibworks.com
Where will you be in 100 years?




Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex pendemv JSP Basics 4 December 8th, 2008 08:29 AM
regex mrame XSLT 12 July 25th, 2008 09:37 AM
Regex htran XSLT 2 May 18th, 2005 09:21 AM
Need RegEx help Snib Pro PHP 6 November 16th, 2004 06:59 AM
Regex Help boyer99g General .NET 2 October 8th, 2004 05:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.