note 109161 added to function.str-replace

From: Date: Mon, 25 Jun 2012 07:35:24 +0000
Subject: note 109161 added to function.str-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-188781@lists.php.net to get a copy of this message
To Kai Hoerner If you want to replace only a specific occurance of a needle, you can use this handy function: I have found a bug in your function. function str_replace_occurance($search, $replace, $subject, $occurance) { $pos = 0; for ($i = 0; $i <= $occurance; $i++) { // Rather than this //$pos = strpos($subject, $search, $pos); // It is better to do this, to avoid a loop into the same result. $pos = strpos($subject, $search, $pos+1); } return substr_replace($subject, $replace, $pos, strlen($search)); } ---- Server IP: 69.147.83.197 Probable Submitter: 93.176.80.34 ---- Manual Page -- http://www.php.net/manual/en/function.str-replace.php Edit -- https://master.php.net/note/edit/109161 Del: integrated -- https://master.php.net/note/delete/109161/integrated Del: useless -- https://master.php.net/note/delete/109161/useless Del: bad code -- https://master.php.net/note/delete/109161/bad+code Del: spam -- https://master.php.net/note/delete/109161/spam Del: non-english -- https://master.php.net/note/delete/109161/non-english Del: in docs -- https://master.php.net/note/delete/109161/in+docs Del: other reasons-- https://master.php.net/note/delete/109161 Reject -- https://master.php.net/note/reject/109161 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#188781) next »