note 109161 deleted from function.str-replace by danbrown
| From: | danbrown@php.net | Date: | Tue, 26 Jun 2012 13:46:03 +0000 |
| Subject: | note 109161 deleted from function.str-replace by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-188816@lists.php.net to get a copy of this message | ||
Note Submitter: njean2001 at yahoo dot com
----
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));
}