For some reason the following regex which does work in .NET is not working in PHP:
// Line breaks not in real expression, just for clarity here to show sub-expressions:
$pattern = <<<REGEX
"(\\"|[^"])*"
|
'(\\'|[^'])*'
|
[A-Za-z_][A-Za-z_\-\d]*
|
[\-\+]?\d+(\.\d+)?
|
[=<>!][=]
|
[?:,()*\/\-\+!]
|
\|\|?
|
\&\&?
REGEX;
Actual expression for testing:
$pattern = '/"(\\\"|[^"])*"|\'(\\\'|[^\'])*\'|[A-Za-z_][A-Za-z_\-\d]*|[\-\+]?\d+(\.\d+)?|[=<>!][=]|[?:,()*\/\-\+!]|\|\|?|\&\&?/';
$expr = <<<EXPR
something ? '<a href="example.com">', title, '</a>' : title
EXPR;
preg_match_all($pattern, $expr, $tokens);
Expected Output for $tokens[0]
0: something
1: ?
2: '<a href="something.com">' // Please note that ' are part of token!
3: ,
4: title
5: ,
6: '</a>'
7: :
8: title
Actual Output for $tokens[0]
0: something
1: ?
2: a
3: href
4: something
5: com
For some reason most of the input is missing and the literal has been ignored. This works in .NET but for some reason is not working in PHP.
The problem is that you need to escape the regex backslash metacharacters. Also, it is best to write out complex regexes in free-spacing mode with lots of comments. Here is the PHP code you need:
Using the
heredocsyntax is not needed or recommended here.