Hi,
I have read PHP bug #15050 and PHP bug #10666. Example at bottom of this
mail.
Andrei states in bug #10666: "The fact that it backslash-escapes single
and double quotes in matches before substituting $1 is a feature, not a
bug, otherwise you'd have a really hard time figuring out which quotes
go where when using evaluation strings."
I assume that the result of '$1' would be
'A "little" test and a 'little' test'
.. evaluating to:
A "little" test and a 'little' test
This approach to handle $1 (by adding slashes) is ugly and pretty much a
hack in my opinion. Is there really no way at all to consider $1 as a
"real" variable? After all, there is no pain in perl running stuff like:
s/(.*)/"$1 [".length($1)." characters]"/e;
.. where the string includes ' and "
As it currently is, any function called in /e would always require a
wrapper to run stripslashes()
- which could also be destructive since
only one of ' and " is escaped in the eval'ed string parsed on to a
function.
This currently render the otherwise powerful /e-feature pretty useless.
At least, the behaviour ought to be mentioned under the /e-flag at
pcre.pattern.modifiers in the manual?
(actually, it seems that the whole adaption of pcre has been performed
without much consideration - even though it has been more structured,
there are still leftovers such as delimiters, which only made sense in
the shorthand-form present in perl, but not in a structured function
with several arguments - flags might also have been moved into an argument
for itself... it's a pity since there are many benefits from preg_*
opposed to ereg_* such as speed, flexibility, more powerful features,
etc...)
Quick example, for the curious: (PHP4.3.8, PHP5.0.0)
<?php
$string = <<<EOD
A "little" test and a 'little' test
EOD;
print preg_replace('_(.)e',"'$1'",$string)."\n";
print preg_replace('(.)_e','"$1"',$string)."\n";
?>
Expected result:
A "little" test and a 'little' test
A "little" test and a 'little' test
Actual result:
A "little" test and a 'little' test
A "little" test and a 'little' test
magic_quotes_runtime is disabled (and unimportant, so is magic_quotes_gpc).
--
- Peter Brodersen
(trying not to be too much of a party pooper :)
I documented this under e modifier: "Single and double quotes are
escaped by backslashes in substituted backreferences."
Decision if the behavior will change is up to the others.
You can use preg_replace_callback()
instead of e modifier. It doesn't
escape quotes and is more PHPish :-).
Jakub Vrána