2

I got the following code

$original_file=str_replace('<?php','',file_get_contents('file.txt'));

//regular expression to get anything that starts with '$' and end with ';'
preg_match_all("/\\$(.*?);/",$original_file,$matches,PREG_PATTERN_ORDER);

I have succeeded in the getting the variables or anything starting with the '$', But now I am trying to modify that regular expression to lookbehind the $ and check if it is preceded by the \ comments keywords. if it is then don't get it.Does someone have a good suggestion? I am using PHP preg_match_all as you can see above. Any help is appreciated.

user1518071
  • 51
  • 2
  • 8
  • 1
    \\ isn't a PHP comment, it's // – AbraCadaver Nov 04 '13 at 18:16
  • 1
    If you are trying to parse PHP in PHP, you may have a look into [this related question](http://stackoverflow.com/questions/5586358/any-decent-php-parser-written-in-php). Or even [token_get_all()](http://php.net/token_get_all) function. – Guillaume Poussel Nov 04 '13 at 18:19
  • Guillaume Poussel, no I am not, but the information is very interesting I may use it in the future. – user1518071 Nov 06 '13 at 20:15

2 Answers2

2

You can try:

preg_match_all("~(?<!//)\$([^;]*);~", $original_file, $matches, PREG_PATTERN_ORDER);
anubhava
  • 713,503
  • 59
  • 514
  • 593
0

As @anubhava but using s option for traversing over all the $str lines,

preg_match_all('/(?<!\\\)\$([^;]+);/s', $str, $m, PREG_PATTERN_ORDER);

To avoid generic match, use the variable syntax (supposing alphanumeric variables like PHP variables),

preg_match_all(
    '/(?<!\\\)\$([a-z0-9_]+);/si', 
    $str, 
    $m, 
    PREG_PATTERN_ORDER
);

without this little enhance, your parser will catch wrong syntax, in the case of

$str = "\nTEST\n\n \$x;\$y;\ny\$y_ \$_X;"; 

For a complete "template parsing" see the Smallest Tempalte System concepts and discussions.

Peter Krauss
  • 12,407
  • 21
  • 149
  • 275
  • Hello @user1518071, it is not only "for precision", check by your self the test (I edited now illustrating with a `$str` value) showing an error case of the "generic match". – Peter Krauss Nov 10 '13 at 12:00