简体   繁体   English

PHP str_replace与数组中的for循环

[英]PHP str_replace with for loop from array

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. 好的,我有一个str_replace,我想做的是从数组中获取值,然后用下一部分替换“ dog”一词。 So basically I want the $string to read: 所以基本上我希望$ string读取:

"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? 有人有想法么?

Edit: Sorry for the erroneous answer earlier. 编辑:对不起,以前的错误答案。 This'll do it. 这样就可以了。 No str_replace , no preg_replace , just raw, fast string searching and splicing: 没有str_replace ,没有preg_replace ,只是原始的,快速的字符串搜索和拼接:

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

ps Not a big deal in this example, but you should keep count() outside your loop. ps在此示例中没什么大不了的,但是您应将count()保留在循环之外。 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. 在for循环的第一次迭代之后,$ string将用dog替换出现的dog,随后的迭代将无效。

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() ; 使用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;
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM