Regular expression to match either a single or double quoted string, taking into account escape characters:
/(["'])((?:(?!\1|\\).|\\.)*?)\1/
It's recommended that you remove escape characters from the resulting string (replace \\((?=\%s|\\).)
with \1
substituting %s
for the matched quote character):
preg_match('/(["\'])((?:(?!\\1|\\\\).|\\\\.)*?)\\1/', $string, $matches);
$extractedStringValue = preg_replace(sprintf('/\\\\((?=\\\\|\\%s).)/', $matches[1]), '\1', $matches[2]);