note 99936 deleted from function.preg-replace by danbrown
| From: | danbrown@php.net | Date: | Thu, 16 Sep 2010 15:54:37 +0000 |
| Subject: | note 99936 deleted from function.preg-replace by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-172654@lists.php.net to get a copy of this message | ||
Note Submitter: abimaelrc at hotmail dot com
----
If you want to use the dollar sign and immediately a number, you need escape the dollar sign. For
example:
<?php
$foo = 'I got $$$$$10';
$bar = 'No it\'s $2';
$foo = preg_replace('/.+/', $bar, $foo);
echo $foo; // returns No it's
?>
The reason is that it is making a back reference of a second subpattern. Check this, for more info
http://www.php.net/manual/en/regexp.reference.back-references.php
If you want that expression to work, you need to escape the dollar sign:
<?php
$foo = 'I got $$$$$10';
$bar = 'No it\'s \$2';
$foo = preg_replace('/.+/', $bar, $foo);
echo $foo; // returns No it's $2
?>