1

I was creating a Syntax Highlighter in PHP but I was failed! You see when I was creating script comments (//) Syntax Highlighting (gray) , I was facing some problems. So I just created a shortened version of my Syntax Highlighting Function to show you all my problem. See whenever a PHP variable ,i.e., $example, is inserted in between the comment it doesn't get grayed as it should be according to my Syntax Highlighter. You see I'm using preg_replace() to achieve this. But the regex of it which I'm using currently doesn't seem to be right. I tried out almost everything that I know about it, but it doesn't work. See the demo code below.

Problem Demo Code

<?php
  $str = '
  <?php
    //This is a php comment $test and resulted bad!
    $text_cool++;
  ?>
  ';
  $result = str_replace(array('<','>','/'),array('[',']','%%'),$str);
  $result = preg_replace("/%%%%(.*?)(?=(\n))/","<span style=\"color:gray;\">$0</span>",$result);
  $result = preg_replace("/(?<!\"|'|%%%%\w\s\t)[\$](?!\()(.*?)(?=(\W))/","<span style=\"color:green;\">$0</span>",$result);
  $result = str_replace(array('[',']','%%'),array('&lt;','&gt;','/'),$result);
  $resultArray = explode("\n",$result);
  foreach ($resultArray as $i) {
    echo $i.'</br>';
  }
?>

Problem Demo Screen

enter image description here

So you see the result I want is that $test in the comment string of the 'Demo Screen' above should also be colored as gray!(See below.)

enter image description here

Can anyone help me solve this problem?

I'm Aware of highlight_string() function!

THANKS IN ADVANCE!

2
  • 2
    As PHP has this facility built in, there is a question of why you are doing it, not that it helps to say that. Commented Jun 24, 2011 at 17:10
  • You're really rolling your own? Why reinvent the wheel? Commented Jun 24, 2011 at 17:11

5 Answers 5

6

Reinventing the wheel?

highlight_string()

Also, this is why they have parsers, and regex (despite popular demand) should not be used as a parser.

Sign up to request clarification or add additional context in comments.

5 Comments

But isn't there a way of doing it with regex because I barely wanted to create my own Syntax Highlighter, which have great color combinations unlike the original php Syntax Highlighter! Please help me out.
@Jack: The solution is to not use regex. Regex is good for some things, but in regards to parsing, it's not. (You'd have to keep making exception patterns to catch instances where it is a variable name, but does not fall within a comment block, for instance).
@Jack: You can parse it to a temp string ($temp = highlight_string(...)) then run a string replace on, e.g. "all colors #FF00FF should become #00FF00" (make your own scheme).
see answer from @cwallenpoole. Use CSS to override default color scheme
@Jack: Use GeSHi, a fully customizable syntax highlighter.
3

I agree, that you should use existing, parsers. Every ide has a php parser, and many people have written more of them.

That said, I do think it is worth the mental exercise. So, you can replace:

$result = preg_replace("/(?<!\"|')[\$](?!\()(.*?)(?=(\W))/","<span style=\"color:green;\">$0</span>",$result);

with

//regular expression.:
//#([^(%%%%|\"|')]*)([\$](?!\()(.*?)(?=(\W)))#

//replacement text:
//$1<span style=\"color:green;\">$2</span>

$result = preg_replace("#([^(%%%%|\"|')]*)([\$](?!\()(.*?)(?=(\W)))#","$1<span style=\"color:green;\">$2</span>",$result);

4 Comments

Did you try it out? I tested it here
Please tell me how to use it?
I've added the full line you should use instead of what you had.
Oh. I did't notice that you were replacing the // with %%%%. Edited with fix.
2

Personally, I think your best bet is to use CSS selectors. Replace style=\"color:gray;\" with class="comment-text" and style=\"color:green;\" with class="variable-text" and this CSS should work for you:

.variable-text {
    color: #00E;
}
.comment-text .comment-text.variable-text {
    color: #DDD;
}

Comments

1

Insert don't use regex to parse irregular languages here

anyway, it looks like you've run into a prime example of why regular expressions are not suited for this kind of problem. You'd be better off looking into PHP's highlight_string functionality

Comments

0

Well, you don't seem to care that php already has a function like this.

But because of the structure of php code one cannot simply use a regex for this or walk into mordor (the latter being the easier).

You have to use a parser or you will fly over the cuckoo's nest soon.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.