<?php
$backslash = '\\';
$pattern = <<< PATTERN
#(["'])(?:{$backslash}{$backslash}?+.)*?{$backslash}1#
PATTERN;
foreach(array(
"<?php \$s = 'Hi everyone, we\\'re ready now.'; ?>",
'<?php $s = "Hi everyone, we\\"re ready now."; ?>',
"xyz'a\\'bc\\d'123",
"x = 'My string ends with with a backslash\\\\';"
) as $subject) {
preg_match($pattern, $subject, $matches);
echo $subject , ' => ', $matches[0], "\n\n";
}
プリント
<?php $s = 'Hi everyone, we\'re ready now.'; ?> => 'Hi everyone, we\'re ready now.'
<?php $s = "Hi everyone, we\"re ready now."; ?> => "Hi everyone, we\"re ready now."
xyz'a\'bc\d'123 => 'a\'bc\d'
x = 'My string ends with with a backslash\\'; => 'My string ends with with a backslash\\'