Next to the two given suggestions, if you're looking for PHP PCRE based regexes to validate a subset of PHP, this can be done more structured by specifying named subpatterns for the tokens you're looking for. Here is an exemplary regular expression pattern that's looking for these patterns even allowing whitespace around (as PHP would do) for any us-ascii based extended single-byte charsets (I think this is how PHP actually treats it even if it's UTF-8 in your files):
~
(?(DEFINE)
(?<stringDoubleQuote> "(?:\\"|[^"])+")
(?<stringSingleQuote> '(?:\\'|[^'])+')
(?<string> (?:(?&stringDoubleQuote)|(?&stringSingleQuote)))
(?<variable> \\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*))
(?<varorstring> (?:(?&variable)|(?&string)))
)
^ \s* (?&varorstring) (?: \s* \. \s* (?&varorstring) )* \s* ; $
~x
Thanks to the named subpatterns it's easy to use a token for any string or variable and add the whitespace handling and string concatenating operator. Such assigned to $pattern
, an example of use is:
$lines = <<<'LINES'
"hiii";
"how"."are"."you";
$var."abc";
"abc".$var;
'how'."how".$var;
LINES;
foreach (explode("\n", $lines) as $subject) {
$result = preg_match($pattern, $subject);
if (FALSE === $result) {
throw new LogicException('PCRE pattern did not compile.');
}
printf("%s %s match.\n", var_export($subject, true), $result ? 'did' : 'did not');
}
Output:
'"hiii";' did match.
'"how"."are"."you";' did match.
'$var."abc";' did match.
'"abc".$var;' did match.
'\'how\'."how".$var;' did match.
Demo: https://eval.in/142721
Related