note 99962 deleted from function.preg-replace by danbrown
| From: | danbrown@php.net | Date: | Fri, 17 Sep 2010 02:38:53 +0000 |
| Subject: | note 99962 deleted from function.preg-replace by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-172666@lists.php.net to get a copy of this message | ||
Note Submitter: abimaelrc
----
If you have the dollar sign in a string and have an immediately a number, you need to escape the
dollar sign. For example
<?php
$dollar = 'I got $20';
$replace = 'No, you got $5';
$string = preg_replace('/.+/', $replace, $dollar);
echo $string; //ouput No, you got
?>
The reason is that preg_replace interpreted the $5 as a
back reference. Specifically as the fifth
subpattern. For more information
http://www.php.net/manual/en/regexp.reference.back-
references.php
To be able to read all the content, you must escape
the dollar sign.
<?php
$dollar = 'I got $20';
$replace = 'No, you got \$5';
$string = preg_replace('/.+/', $replace, $dollar);
echo $string; //ouput No, you got $5
?>