views:

692

answers:

5

Ok I have a str_replace and what I want to do, is take values from an array, and take the next piece to replace the word "dog" with. So basically I want the $string to read:

"The duck ate the cat and the pig ate the chimp"

<?php
$string = 'The dog ate the cat and the dog ate the chimp';
$array = array('duck','pig');
for($i=0;$i<count($array);$i++) {
    $string = str_replace("dog",$array[$i],$string);
}
echo $string;
?>

This code just returns:

"The duck ate the cat and the duck ate the chimp"

I tried several things but nothing works. Anyone have any ideas?

+4  A: 

Edit: Sorry for the erroneous answer earlier. This'll do it. No str_replace, no preg_replace, just raw, fast string searching and splicing:

<?php
$string = 'The dog ate the cat and the dog ate the chimp';
$array = array('duck', 'pig');
$count = count($array);
$search = 'dog';
$searchlen = strlen($search);
$newstring = '';
$offset = 0;
for($i = 0; $i < $count; $i++) {
    if (($pos = strpos($string, $search, $offset)) !== false){
        $newstring .= substr($string, $offset, $pos-$offset) . $array[$i];
        $offset = $pos + $searchlen;
    }
}
$newstring .= substr($string, $offset);
echo $newstring;
?>

p.s. Not a big deal in this example, but you should keep count() outside your loop. With it where you had it, it gets executed every iteration and is slower than just calling it once beforehand.

brianreavis
The 4th parameter to str_replace has to be a variable and it will be populated with the number of times a replacement happened. Not what you want.
camomileCase
D'oh. Went too fast through the docs...
brianreavis
i replaced str_replace with preg_replace because that uses limit, and now i get "The duck ate the cat and the dog ate the chimp". its not replacing the second "dog" =[
David
+1  A: 

After the first iteration of your for loop $string will have replaced both occurences of dog with duck and the following iterations will do nothing.

I can't think of a more elegant way of solving this and I hope there is something simpler possible:

<?php

$search = 'The dog ate the cat and the dog ate the chimp';
$replacements = array('duck','pig');
$matchCount = 0;
$replace = 'dog';

while(false !== strpos($search, $replace))
{
  $replacement = $replacements[$matchCount % count($replacements)];
  $search = preg_replace('/('.$replace.')/', $replacement, $search, 1);
  $matchCount++;
}

echo $search;
camomileCase
Wow hey at least it works, I'll go over this tonight so I understand it fully. =] thanks very much!!!
David
+2  A: 
<?php
$string = 'The dog ate the cat and the dog ate the chimp';
$array = array('duck', 'pig');

$count = count($array);

for($i = 0; $i < $count; $i++) {
    $string = preg_replace('/dog/', $array[$i], $string, 1);
}

echo $string;
?>

The duck ate the cat and the pig ate the chimp

lemon
Oh I think I get it, if you keep the same variable "$string" throughout all the statements it will work I assume?
David
It'll work. First loop $str = The duck ate the cat and the dog ate the chimp. Second loop The duck ate the cat and the pig ate the chimp.
lemon
A: 

yet one option

 $str = 'The dog ate the cat and the dog ate the chimp';
 $rep = array('duck','pig');
 echo preg_replace('/dog/e', 'array_shift($rep)', $str);
stereofrog
A: 

Using substr_replace();

<?php
function str_replace_once($needle, $replace, $subject)
{
    $pos = strpos($subject, $needle);
    if ($pos !== false)
        $subject = substr_replace($subject, $replace, $pos, strlen($needle));
    return $subject;
}

$subject = 'The dog ate the cat and the dog ate the chimp';
$subject = str_replace_once('dog', 'duck', $subject);
$subject = str_replace_once('dog', 'pig', $subject);

echo $subject;
?>
ntd