Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php str_replace()搜索时重复单词_Php_Str Replace - Fatal编程技术网

php str_replace()搜索时重复单词

php str_replace()搜索时重复单词,php,str-replace,Php,Str Replace,正在尝试使用str_replace搜索和替换某些内容。如果$find是唯一的,那么它可以正常工作,但是如果$find有重复的单词,那么整个过程就会一团糟 e、 g.样式1将应用于$find[0]&&$find[1],因为两者都是Lorem Ipsum,dummy也是如此 如何处理这种情况 $find = Array( [0] => Lorem Ipsum [1] => Lorem Ipsum [2] => typesetting [3] =

正在尝试使用str_replace搜索和替换某些内容。如果$find是唯一的,那么它可以正常工作,但是如果$find有重复的单词,那么整个过程就会一团糟

e、 g.样式1将应用于$find[0]&&$find[1],因为两者都是Lorem Ipsum,dummy也是如此

如何处理这种情况

$find = Array(
    [0] => Lorem Ipsum 
    [1] => Lorem Ipsum 
    [2] => typesetting 
    [3] => dummy 
    [4] => dummy 
);

$replace = Array(
    [0] => style1
    [1] => style2
    [2] => style3
    [3] => style4
    [4] => style5
);

$string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
           Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, 
           when an unknown printer took a galley of type and scrambled it to make a type specimen book.";

$result = str_replace($find,$replace,$string);
echo $result;

您可以先使用str_replace_的实现,如中所述,然后迭代$find和$replace数组,仅替换$find数组中每个值的一个匹配项:

function str_replace_first($search, $replace, $subject) {
    if (($pos = strpos($subject, $search)) !== false) {
        return substr_replace($subject, $replace, $pos, strlen($search));
    }
    return $subject;
}

$result = $string;
foreach ($find as $key => $search) {
    $result = str_replace_first($search, $replace[$key], $result);
}
echo $result;
输出:

style1 is simply style4 text of the printing and style3 industry. 
style2 has been the industry's standard style5 text ever since the 1500s, 
when an unknown printer took a galley of type and scrambled it to make a type specimen book.

如果存在重复项,您预计会发生什么?是否希望第一次出现的Lorem Ipsum替换为style1,第二次替换为style2?@Nigel Ren抱歉没有提到预期的输出。应该类似于查找[0]替换为样式[0],查找[1]替换为样式[1]。在使用@Nick中的代码迭代$find和$replace数组时,只需替换第一个匹配项。我尝试了您建议的方法,但$string也在迭代Hanks,我尝试了RedYetiCo的ans@Max别担心。确保你在链接页面上对僵尸的答案投了赞成票。