note 125026 deleted from function.preg-replace by crell
| From: | crell@php.net | Date: | Fri, 01 Jul 2022 20:08:18 +0000 |
| Subject: | note 125026 deleted from function.preg-replace by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218879@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
Note that the replacement string using the ${n} notation for a group identifier (rather than the $n
notation) must be in a SINGLE quoted string:
$replacement = '${1}1,$3'; // Correct - Single Quoted
$replacement = "${1}1,$3"; // Incorrect - Double Quoted
$replacement = "$1 one , $3"; // Correct - Double Quoted but with $n notation.
This might sneak up on you, because it is not an error to use "${1}". The double quotes
allow a string that contains variable interpolation, and the braces make the variable a PHP
"Variable-Variable". In the above example the undefined ${1} will evaluate to NULL rather
than the replaced string. Valid PHP, but not what you wanted.