|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2019-12-29 15:52 UTC] thebluewalrus at gmail dot com
Description:
------------
Using curly braces and a backreference in double quotes in regex replacement produces a 'T_LNUMBER' error for an invalid variable name. This behavior is inconsistent with other behavior though, e.g. uncurled the "variable" doesn't throw parser error.
Test script:
---------------
Correct behavior:
preg_replace('/(a)/', "$1test", 'a');
Incorrect behavior:
preg_replace('/(a)/', "{$1}test", 'a');
Expected result:
----------------
In regex context this behaviors is incorrect because '{$3}' should produce '{capture group three value}', as it does outside the curly braces.
Actual result:
--------------
Parse error: syntax error, unexpected '1' (T_LNUMBER), expecting variable (T_VARIABLE) or '{' or '$'
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 21:00:01 2025 UTC |
you can't have variables starting with a number in PHP ----------------------- Correct behavior: preg_replace('/(a)/', "$1test", 'a'); i would say this is not correct behavior and should trhow the same error becasue you can't have a variable $1test nor can you have $1 ----------------------- php > $1test = 'test'; Parse error: syntax error, unexpected '1' (T_LNUMBER), expecting variable (T_VARIABLE) or '{' or '$' in php shell code on line 1Yes, I agree I would expect '"$1"' and '"{$1}"' to behave the same but they don't. I think one or the other is functioning incorrectly. Simplifying the "correct" example it still executes: preg_replace('/(a)/', "$1", 'a');irrelevant, the second one should throw the same error than the first and that can even throw at compile time php > echo $1test; Parse error: syntax error, unexpected '1' (T_LNUMBER), expecting variable (T_VARIABLE) or '{' or '$' in php shell code on line 1 php > echo "$1test"; $1testSo the 'complex syntax' is not as advanced as the regular variable processor but the behavior of the 'complex syntax' will not be corrected because that is how it has been for ages? It would seem to me that \{\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\} would be better than \{\$[^}]+?\} which is what I presume the regular variable processor uses. If it's not a variable it shouldn't process it. I can't see how this would break past functionality, Previously the complex failed in instance where it shouldn't have.Wouldn't that mean munging the input to PCRE, because it's the library specifying the use of "$1" to refer to pattern replacements? (And you can blame Perl for that.) Don't forget to also handle its use of {} to disambiguate between ${1}0 and $10. Just one MORE reason to prefer single quotes when writing string literals in those function calls.