note 99962 added to function.preg-replace

From: Date: Fri, 17 Sep 2010 02:31:47 +0000
Subject: note 99962 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-172665@lists.php.net to get a copy of this message
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 ?> ---- Server IP: 69.147.83.197 Probable Submitter: 67.224.182.48 ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace.php Edit -- https://master.php.net/note/edit/99962 Del: integrated -- https://master.php.net/note/delete/99962/integrated Del: useless -- https://master.php.net/note/delete/99962/useless Del: bad code -- https://master.php.net/note/delete/99962/bad+code Del: spam -- https://master.php.net/note/delete/99962/spam Del: non-english -- https://master.php.net/note/delete/99962/non-english Del: in docs -- https://master.php.net/note/delete/99962/in+docs Del: other reasons-- https://master.php.net/note/delete/99962 Reject -- https://master.php.net/note/reject/99962 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#172665) next »