Q:

Regex not working in PHP same as with C#

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. 

A:

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:
<? php
$pattern = '%
      "(\\\\"|[^"])*"          # Either... a double quoted string,
    |                          # or...
      \'(\\\\\'|[^\'])*\'      # a single quoted string,
    |                          # or...
      [A-Za-z_][A-Za-z_\-\d]*  # an identifier. 
    |                          # or...
      [\-\+]?\d+(\.\d+)?        # a number
    |                          # or...
      [=<>!] [=]                # a comparison operator,
    |                          # or...
      [?:,()* \/\-\+!]           # a single char 
    |                          # or...
      \|\|?                     # a logical or numerical OR
    |                          # or...
    \&\&?                       # a logical or numerical AND
    %ix';

$expr = <<<EXPR
something ? ' <a href="example.com">', title, '</a>' : title
EXPR;

$a = preg_match_all($pattern, $expr, $tokens);
print_r($tokens[0]);

? >

Using the heredoc syntax is not needed or recommended here.