if you use preg_replace with the \e modifier and pass some captured text to a function you will notice the following behaviour:
if you pass the replacement parameter (\1) in double quotes, all single quotes in the string are getting escaped, if you pass the parameter in single quotes, all double quotes are getting escaped.
as an example look at the following code:
function showText($info, $in) {
echo $info.': '.$in."\n";
}
$pattern = '/#(.*)#/e';
$subject = '#\'quote\' - "double quote" - \\backslash\\#';
preg_replace($pattern, 'showText(\'test1.1\', \'\1\')', $subject);
preg_replace($pattern, 'showText(\'test1.2\', "\1")', $subject);
preg_replace($pattern, "showText('test1.3', '\\1')", $subject);
preg_replace($pattern, "showText('test1.4', \"\\1\")", $subject);
// output:
// test1.1: 'quote' - \"double quote\" - \backslash\
// test1.2: \'quote\' - "double quote" - \backslash\
// test1.3: 'quote' - \"double quote\" - \backslash\
// test1.4: \'quote\' - "double quote" - \backslash\
consequences: you might have to take care about the extra escaped single or double quotes in the function you are calling. Unfortunatly this depends on the way you add the capture text into the function call - so there is no way of knowing within the function what to strip away...
Partager