note 94073 added to function.str-replace

From: Date: Thu, 15 Oct 2009 08:41:18 +0000
Subject: note 94073 added to function.str-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-161750@lists.php.net to get a copy of this message
As mentioned earlier you should take the order into account when substituting multiple values. However it is worth noticing that str_replace doesn't seem to re-read the string when doing single replacements. Take the following example. <?php $s = '/a/a/'; $s = str_replace('/a/', '/', $s); ?> You would expect the following. First replacement '/a/a/' -> '/a/' Second replacement '/a/'->'/' This is not the case, the actual result will be '/a/'. To fix this, you will have to put str_replace in a while-loop. <?php $s = '/a/a/'; while(strpos($s, '/a/') !== false) $s = str_replace('/a/', '/', $s); //eventually $s will == '/' ?> ---- Server IP: 69.147.83.197 Probable Submitter: 217.74.211.98 ---- Manual Page -- http://www.php.net/manual/en/function.str-replace.php Edit -- https://master.php.net/note/edit/94073 Del: integrated -- https://master.php.net/note/delete/94073/integrated Del: useless -- https://master.php.net/note/delete/94073/useless Del: bad code -- https://master.php.net/note/delete/94073/bad+code Del: spam -- https://master.php.net/note/delete/94073/spam Del: non-english -- https://master.php.net/note/delete/94073/non-english Del: in docs -- https://master.php.net/note/delete/94073/in+docs Del: other reasons-- https://master.php.net/note/delete/94073 Reject -- https://master.php.net/note/reject/94073 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#161750) next »