Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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?
Reply With Quote
  #2 (permalink)  
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?
Reply With Quote
  #3 (permalink)  
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?
Reply With Quote
  #4 (permalink)  
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...
Reply With Quote
  #5 (permalink)  
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?
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 07:31 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.