note 125026 added to function.preg-replace
| From: | Anonymous@php.net | Date: | Tue, 19 May 2020 06:28:10 +0000 |
| Subject: | note 125026 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-214652@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 45.112.84.4
Probable Submitter: 38.142.137.34 (proxied: 2001:569:72cc:6100:e950:8412:a11b:ab7f)
----
Manual Page -- http://php.net/manual/en/function.preg-replace.php
Edit -- https://master.php.net/note/edit/125026
Del: integrated -- https://master.php.net/note/delete/125026/integrated
Del: useless -- https://master.php.net/note/delete/125026/useless
Del: bad code -- https://master.php.net/note/delete/125026/bad+code
Del: spam -- https://master.php.net/note/delete/125026/spam
Del: non-english -- https://master.php.net/note/delete/125026/non-english
Del: in docs -- https://master.php.net/note/delete/125026/in+docs
Del: other reasons-- https://master.php.net/note/delete/125026
Reject -- https://master.php.net/note/reject/125026
Search -- https://master.php.net/manage/user-notes.php