0

I have a str_replace() call and I want to 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.

1
  • You could use a combination of strstr and substr_replace I guess. Commented Nov 14, 2009 at 6:09

6 Answers 6

5

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.

2
  • 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. Commented Nov 14, 2009 at 5:57
  • 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" =[ Commented Nov 14, 2009 at 5:59
2
<?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

2
  • Oh I think I get it, if you keep the same variable "$string" throughout all the statements it will work I assume? Commented Nov 14, 2009 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. Commented Nov 14, 2009 at 7:30
1

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;
0
0

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);
1
  • This is no longer valid PHP. Commented Aug 12, 2024 at 22:32
0

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;
?>
0

Make solitary replacements while consuming the replacement array in a loop.

Break the loop when the replacement array runs out of values or when there are no replacements made.

If the $find value comes from an untrusted source, call preg_quote() on it before feeding it to preg_replace().

Code: (Demo)

$string = 'The dog ate the cat and the dog ate the chimp';
$find = 'dog';
$array = ['duck', 'pig'];
$count = 1;

while ($array && $count) {
    $string = preg_replace("#$find#", array_shift($array), $string, 1, $count);
}
echo $string;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.