How can preg_match_all be used to extract all strings within double quotes in PHP, considering escaped double quotes within the string?

When using preg_match_all to extract strings within double quotes in PHP, we need to consider the possibility of escaped double quotes within the string. To handle this, we can use a regular expression pattern that matches both unescaped and escaped double quotes within the string. By using a combination of lookahead and lookbehind assertions, we can successfully extract all strings within double quotes, including those with escaped double quotes.

$str = 'This is a "test\" string" with "escaped\" double quotes"';
preg_match_all('/(?<=["\s]|^)"((?:[^"\\\\]|\\\\.)*)"/', $str, $matches);
print_r($matches[1]);