Current location: Hot Scripts Forums » Programming Languages » PHP » Problem with multiple expressions

Problem with multiple expressions

Reply
  #1 (permalink)  
Old 06-29-09, 02:25 AM
TehBigA TehBigA is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Problem with multiple expressions

I'm trying to make a fairly simple syntax highlighter that just matches a few things but I'm not sure how to match multiple expressions, such as I don't want to match a number if is within a comment or quoted string.

For example my text:
Code:
// A comment with numbers, 123, and string, "STRING"
Right now I have these expressions:
> Numbers: ~[0-9]+(?:\.[0-9]*)?~m
> Comments: ~(\/\/.*$)|((\/\*[\s\S]*?\*\/))~m
> Strings: ~(?<!\w)(['"])(.*)\1(?!\w)~m
> Special: ~\b(function|local|true|false|if|then|end|for|do|w hile|break|return)\b~m

EDIT: I'm not sure why the forum is putting a space between |while|

Last edited by TehBigA; 06-29-09 at 02:28 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 06-29-09, 04:29 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 7,536
Thanks: 5
Thanked 18 Times in 17 Posts
Are you aware of PHP's built-in syntax highlighter? - PHP: highlight_string - Manual

I'd also take a look at GeSHi: GeSHi - Generic Syntax Highlighter :: Home
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 06-29-09, 04:58 AM
TehBigA TehBigA is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Yes I know of both, I just didn't want to include a whole bunch of stuff or libraries that happen to include a highlighter for a one page script, so I started writing my own.

I know pretty much everything to do except a way to check if a number is in a quote or comment and such.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 06-29-09, 06:16 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 7,536
Thanks: 5
Thanked 18 Times in 17 Posts
The built-in highlighter is just one line of code. You can also customize the colors via ini_set(). I don¡'t see what's wrong with using it? It'll be faster, and more precise than your own.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 06-29-09, 11:38 AM
TehBigA TehBigA is offline
Newbie Coder
 
Join Date: Jun 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Well I'm not using it for PHP code, I'm displaying Lua. True the built in would be the absolute best but its a different code.

I probably could just preg_match_all(...) for the differences and change the <font> to span and what not.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 06-29-09, 03:54 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 2,832
Thanks: 13
Thanked 10 Times in 9 Posts
Quote:
Originally Posted by TehBigA View Post
Well I'm not using it for PHP code, I'm displaying Lua.
GeSHi does Lua. You can save yourself a lot of time and agony reinventing the wheel by using it.

I use GeSHi on a site that does PHP, mySQL, Javascript, and Oracle's PL/SQL, and it works flawlessly.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data (scroll down)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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 On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular expressions problem in php EvilDeveloper PHP 2 07-13-08 07:02 AM
mysql multiple table update problem ordique Database 12 07-10-08 04:06 AM
where clause problem!, multiple condition problem djnaf PHP 5 03-25-06 08:16 PM
Date/Time format & multiple selection with listbox problem in ASP.net & C# frmsasp ASP.NET 0 10-13-05 07:07 AM
Search database with multiple AND problem Bonzo PHP 8 07-14-05 03:09 PM


All times are GMT -5. The time now is 01:52 AM.
vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.