I am trying to match quoted strings within a piece of text and allowing for escaped quotes within it as well. I tried this regular expression in an online tester, and it works perfectly. However, when I try it in preg_match_all, it fails at the first escaped string.
Here is the code:
$parStr = 'title="My Little Website"
year="2007"
description="Basic website with ..."
tech="PHP, mySQL"
link="<a href=\"http://test.com\">test.com</a>"
';
$matches = array();
preg_match_all('/(\w+)\s*=\s*"(([^\\"]*(\\.)?)*)"/', $parStr, $matches, PREG_SET_ORDER); // Match[4][0] is 'link="<a href=\"'
It fails on the last match, only matching up until the first escaped quote. When I try this expression at http://www.regexplanet.com/simple/index.html, it works perfectly.
The pertinent part of the regex is:
"(([^\\"]*(\\.)?)*)"
Which should eat all text leading up to an escaped quote or quote, followed by eating an optional escaped quote, of which process is repeated 0 or more times, until a non-escaped quote is found, in which the match is complete.
Why will this not work in php? Why does it not work and how should it be fixed?