开发者

PHP str_replace with for loop from array

开发者 https://www.devze.com 2022-12-11 05:03 出处:网络
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:

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);
}
开发者_C百科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?


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.


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


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;


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


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

精彩评论

暂无评论...
验证码 换一张
取 消