I am usually pretty good to write simple regular expressions, but this time I cannot figure this out.
I need a regular expression that catches all the possible options:
<?php echo [^$]
<?php print [^$]
<? echo [^$]
<? print [^$]
<?echo [^$]
<?print [^$]
<?= [^$]
<?=[^$]
the [^$] stands for “non a $ character”
In other words I want to catch all the times that a print is done not using a variable.
I came up with something like this:
preg_match('/<\?[\s*|=|php]?\s*[echo\s|print\s]?\s*[^\$]/i',$content);
it doesn’t work, can’t figure out what would be the way to write it..
This is because regular expressions in the short form are line noise. No one should have to try to program them, and especially no one should have to try to read them.
Try using the extended flag
/xso you can use white space and add comments, both for others and for your future self.This could be improved by someone better at regular expressions, but it gives you an example of how to start:
Also, an online tool can help you build it up and see the results step by step:
http://gskinner.com/RegExr/
You can see a more detailed example of how to do this in php at the nettuts+ article on Advanced Regular Expressions Tips and Techniques.