note 77558 deleted from function.str-replace by nlopess
| From: | nlopess@php.net | Date: | Tue, 04 Sep 2007 10:00:31 +0000 |
| Subject: | note 77558 deleted from function.str-replace by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-130147@lists.php.net to get a copy of this message | ||
Note Submitter: Stigg
----
Here's a function which replaces every other occurrence of $victim with $invdr and uses no
regular expressions.
function replace2($string, $victim, $invdr){
$invdr_length = strlen($invdr);
$pos = 0;
$i = 3;
while (!($pos = strpos($string, $victim, $pos)) === FALSE){
if ($i%2) $string = substr_replace($string, $invdr, $pos, $invdr_length);
$i++;
if ($i%2) $pos = $pos + $invdr_length;
}
return $string;
}