Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
You could use a combination of strstr and substr_replace I guess. – Mark Nov 14 '09 at 6:09
add comment (requires an account with 50 reputation)

5 Answers

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;
?>
share|improve this answer
add comment (requires an account with 50 reputation)

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);
share|improve this answer
add comment (requires an account with 50 reputation)

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.

share|improve this answer
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 Nov 14 '09 at 5:57
D'oh. Went too fast through the docs... – brianreavis Nov 14 '09 at 5:59
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 Nov 14 '09 at 5:59
add comment (requires an account with 50 reputation)
<?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

share|improve this answer
Oh I think I get it, if you keep the same variable "$string" throughout all the statements it will work I assume? – David Nov 14 '09 at 6:29
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 Nov 14 '09 at 7:30
add comment (requires an account with 50 reputation)

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;
share|improve this answer
Wow hey at least it works, I'll go over this tonight so I understand it fully. =] thanks very much!!! – David Nov 14 '09 at 6:01
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.