UPDATE: It turned out that what broke my reg expressions after escaping the quotes was the / deliminator terminating the expression early.
I'm attempting to store a regex pattern as a string like so:
$link_pattern = "/<a\s+href=(?:"([^"]+)"|'([^']+)').*?>(.*?)</a>/";
However I'm unable to properly encapsulate it since the regex pattern contains both single and double quotes. I understand that normally you can escape the quotes by preceding it with a backslash, but that seems to break the regex.
Best How To :
The quotes are an issue but not the issue you are running into when you escape them. Your delimiter is terminating your regex just before the closing
a which is giving you the
unknown modifier error. It appears you don't have error reporting on though so you aren't seeing that.
PHP sees regex as:
Then tries to read the rest as modifiers:
a is not a modifier, so you get a warning.
$link_pattern = "/<a\s+href=(?:\"([^\"]+)\"|'([^']+)').*?>(.*?)<\/a>/";
$test= '<a href="wolf">wolf2</a>';
preg_match($link_pattern, $test, $matches);
 => <a href="wolf">wolf2</a>
 => wolf
 => wolf2
You could alternatively use a different delimiter, http://php.net/manual/en/regexp.reference.delimiters.php.
In the future I'd recommend against using
/ in a regex where HTML or a URL is going to be present.