开发者

PHP Regex problem!

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 =开发者_Python百科 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

PHP Regex problem!

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.)

PHP Regex problem!

Can anyone help me solve this problem?

I'm Aware of highlight_string() function!

THANKS IN ADVANCE!


Reinventing the wheel?

highlight_string()

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


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);


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;
}


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜