PHP 字符串替换匹配数组中的整个单词
PHP string replace match whole word in array
我想用它的等价物替换数组中包含的完整单词
$arrWords = array(
"cat" => "dog",
"dog" => "mouse",
"phone" => "book"
);
$txttest = "My catalogue has a cat and a phone";
猫应该变成狗而不是老鼠,并且必须不区分大小写
本文
My catalogue has a Cat and a phone
应该变成
My catalogue has a dog and a book
有了这个
$result = str_replace(array_keys($arrWords), array_values($arrWords), $txttest);
我明白了
My mousealogue has a mouse and a book
这不是我所期望的
编辑:我的问题已被标记为与此重复
PHP string replace match whole word
在那个问题中,只需要用另一个词替换一个词,而我需要替换多个词,搜索第一次出现的 array(key) 并替换为 array(value)
这应该适合你
$arrWords = array(
"cat" => "dog",
"dog" => "mouse",
"phone" => "book"
);
$txttest = "My catalogue has a cat and a phone";
$stringArr = explode(' ',$txttest);
foreach($stringArr as $k=>$v){
if(array_key_exists($v,$arrWords)){
$stringArr[$k]=$arrWords[$v];
}
}
echo implode(' ',$stringArr);
编辑
根据给定的情况替换字符串
My catalog has a cat and a phone
使用以下数组匹配键并替换为相应的值
$arrWords = array(
"cat" => "dog",
"dog" => "mouse",
"phone" => "book"
);
由于存在与键匹配的特定值,如果我们在上述数组的 for 循环中使用 preg_replace
,我们的替换字符串很可能会相互替换,
可能的嫌疑人("cat" => "dog","dog" => "mouse")
在这种情况下,一个更好的选择是 preg_replace_callback
和 word boundary
\b
满足这样的要求,因为除了像 [=20 这样的词之外,字符串中也可能有特殊字符=] 即 TAB
或任何其他。
所以理想情况下,不应(实际上永远不会)使用上面提供的原始解决方案,并且不是真正的解决方案,因为当有特殊字符时它会失败.
实际上应该做的是首先创建一个正则表达式
should use word boundary with a capturing group inside containing
all the words to be matched as alternatives(using OR operator).
因此,如果我将上面的语句转换为正则表达式,并牢记给定的搜索和替换数组 $arrWords
,它就像。
/\b(cat|dog|phone)\b/gi
修饰符 g
最后是 global pattern flag
确保所有匹配(不要在第一次匹配后 return )和 i
用于不区分大小写的匹配。在 preg_replace_callback()
函数中使用时,我们可以省略 g
修饰符,因为该函数的第 4 个参数将处理它。
因此,如果我将我的 regex
添加到任何在线正则表达式实用程序并提供类似
的字符串
My catalog has a cat cat cat cat a\tdog and a phone
(请注意,使用在线实用程序时,您应该在将字符串写入正则表达式在线编辑器时使用实际 TAB 删除 \t 以被检测为特殊字符或 \t)
它将突出显示所有找到的匹配项,如下所示
我的目录有一个cat
cat
cat
cat
一个dog
和一个phone
(a
后面的空格是因为TAB
)
现在它们需要被替换,并且在替换上面的语句之后应该被转换为
My catalog has a dog dog dog dog a mouse and a book
所以现在出现了 preg_replace_callback()
和回调函数,它们将完成替换和处理 not swapping/substituting 替换键的技巧。
$r = preg_replace_callback(
"/\b(".implode("|",array_keys($arrWords)).")\b/i",
function($matchingPharse) use ($arrWords) {
return strtolower($arrWords[strtolower($matchingPharse[0])]);
},
$txttest
);
我们在这里做的是,在第一个参数中,我们通过使用 array_keys()
从给定数组中提取要在字符串中匹配的单词,从而使上面的 regex
相同,如preg_replace_callback()
在字符串中找到匹配的单词,它将它们传递给第二个参数中的 callable anonymous function
并且 return 仅在替换数组 $arrWords
中为这些匹配项提供替换单词一个新的东西是 use
关键字,因为我们需要访问 $arrWords
数组和 return 来自匹配键的相应值,回调函数只有一个参数 matches
所以向回调函数传递多个参数的最简单方法是使用 'use' 关键字。我从 here 部分用户贡献的注释下得到了关于它的想法。
所以上面会输出
我的目录有一只狗狗一只老鼠和一本书
如果我们需要检查它是否保持不区分大小写,我们需要添加 i
提要
输入
The cat's mouse(named Mouse), cat (named Cat), and dog (named Giraffe) are in this catalog.
输出
The dog's mouse(named Mouse), dog (named dog), and mouse (named Giraffe) are in this catalog.
高级会员提供的另一个案例是
$arrWords=["cat"=>"dog","dog"=>"mouse","mouse"=>"Mickey"];
输入
The cat's mouse(named Mouse), cat (named Cat), and dog (named Giraffe) are in this catalog.
输出
The dog's mickey(named mickey), dog (named dog), and mouse (named Giraffe) are in this catalog.
我与社区的一位资深成员进行了讨论,他指导我了解原始答案中错误信息的严重性,这些错误信息可能会将其他人引向错误的方向,无论是我还是本社区的任何其他成员社区打算这样做,所以我尝试改进答案,如果 OP 所有者打算使用我以前的答案,他会要求他遵循这种方法。我很抱歉一开始就提供了一个糟糕的解决方案。
我想用它的等价物替换数组中包含的完整单词
$arrWords = array(
"cat" => "dog",
"dog" => "mouse",
"phone" => "book"
);
$txttest = "My catalogue has a cat and a phone";
猫应该变成狗而不是老鼠,并且必须不区分大小写
本文
My catalogue has a Cat and a phone
应该变成
My catalogue has a dog and a book
有了这个
$result = str_replace(array_keys($arrWords), array_values($arrWords), $txttest);
我明白了
My mousealogue has a mouse and a book
这不是我所期望的
编辑:我的问题已被标记为与此重复 PHP string replace match whole word
在那个问题中,只需要用另一个词替换一个词,而我需要替换多个词,搜索第一次出现的 array(key) 并替换为 array(value)
这应该适合你
$arrWords = array(
"cat" => "dog",
"dog" => "mouse",
"phone" => "book"
);
$txttest = "My catalogue has a cat and a phone";
$stringArr = explode(' ',$txttest);
foreach($stringArr as $k=>$v){
if(array_key_exists($v,$arrWords)){
$stringArr[$k]=$arrWords[$v];
}
}
echo implode(' ',$stringArr);
编辑
根据给定的情况替换字符串
My catalog has a cat and a phone
使用以下数组匹配键并替换为相应的值
$arrWords = array(
"cat" => "dog",
"dog" => "mouse",
"phone" => "book"
);
由于存在与键匹配的特定值,如果我们在上述数组的 for 循环中使用 preg_replace
,我们的替换字符串很可能会相互替换,
可能的嫌疑人("cat" => "dog","dog" => "mouse")
在这种情况下,一个更好的选择是 preg_replace_callback
和 word boundary
\b
满足这样的要求,因为除了像 [=20 这样的词之外,字符串中也可能有特殊字符=] 即 TAB
或任何其他。
所以理想情况下,不应(实际上永远不会)使用上面提供的原始解决方案,并且不是真正的解决方案,因为当有特殊字符时它会失败.
实际上应该做的是首先创建一个正则表达式
should use word boundary with a capturing group inside containing all the words to be matched as alternatives(using OR operator).
因此,如果我将上面的语句转换为正则表达式,并牢记给定的搜索和替换数组 $arrWords
,它就像。
/\b(cat|dog|phone)\b/gi
修饰符 g
最后是 global pattern flag
确保所有匹配(不要在第一次匹配后 return )和 i
用于不区分大小写的匹配。在 preg_replace_callback()
函数中使用时,我们可以省略 g
修饰符,因为该函数的第 4 个参数将处理它。
因此,如果我将我的 regex
添加到任何在线正则表达式实用程序并提供类似
My catalog has a cat cat cat cat a\tdog and a phone
(请注意,使用在线实用程序时,您应该在将字符串写入正则表达式在线编辑器时使用实际 TAB 删除 \t 以被检测为特殊字符或 \t)
它将突出显示所有找到的匹配项,如下所示
我的目录有一个cat
cat
cat
cat
一个dog
和一个phone
(a
后面的空格是因为TAB
)
现在它们需要被替换,并且在替换上面的语句之后应该被转换为
My catalog has a dog dog dog dog a mouse and a book
所以现在出现了 preg_replace_callback()
和回调函数,它们将完成替换和处理 not swapping/substituting 替换键的技巧。
$r = preg_replace_callback(
"/\b(".implode("|",array_keys($arrWords)).")\b/i",
function($matchingPharse) use ($arrWords) {
return strtolower($arrWords[strtolower($matchingPharse[0])]);
},
$txttest
);
我们在这里做的是,在第一个参数中,我们通过使用 array_keys()
从给定数组中提取要在字符串中匹配的单词,从而使上面的 regex
相同,如preg_replace_callback()
在字符串中找到匹配的单词,它将它们传递给第二个参数中的 callable anonymous function
并且 return 仅在替换数组 $arrWords
中为这些匹配项提供替换单词一个新的东西是 use
关键字,因为我们需要访问 $arrWords
数组和 return 来自匹配键的相应值,回调函数只有一个参数 matches
所以向回调函数传递多个参数的最简单方法是使用 'use' 关键字。我从 here 部分用户贡献的注释下得到了关于它的想法。
所以上面会输出
我的目录有一只狗狗一只老鼠和一本书
如果我们需要检查它是否保持不区分大小写,我们需要添加 i
提要
输入
The cat's mouse(named Mouse), cat (named Cat), and dog (named Giraffe) are in this catalog.
输出
The dog's mouse(named Mouse), dog (named dog), and mouse (named Giraffe) are in this catalog.
高级会员提供的另一个案例是
$arrWords=["cat"=>"dog","dog"=>"mouse","mouse"=>"Mickey"];
输入
The cat's mouse(named Mouse), cat (named Cat), and dog (named Giraffe) are in this catalog.
输出
The dog's mickey(named mickey), dog (named dog), and mouse (named Giraffe) are in this catalog.
我与社区的一位资深成员进行了讨论,他指导我了解原始答案中错误信息的严重性,这些错误信息可能会将其他人引向错误的方向,无论是我还是本社区的任何其他成员社区打算这样做,所以我尝试改进答案,如果 OP 所有者打算使用我以前的答案,他会要求他遵循这种方法。我很抱歉一开始就提供了一个糟糕的解决方案。