gpt4 book ai didi

php - 使用正则表达式查找字符串中的单词数组

转载 作者:行者123 更新时间:2023-11-29 23:46:28 27 4
gpt4 key购买 nike

我正在寻找字符串中的一些单词,下面是我的代码

$str="Chup Raho Episode 5 by Ary Digital 16th September 2014";

$keywords=array('Ary digital','geo');

echo in_string($keywords,$str,'all');


function in_string($words, $string, $option)
{
if ($option == "all") {
$isFound = true;
foreach ($words as $value) {
$isFound = $isFound && (stripos($string, $value) !== false); // returns boolean false if nothing is found, not 0
if (!$isFound) break; // if a word wasn't found, there is no need to continue
}
} else {
$isFound = false;
foreach ($words as $value) {
$isFound = $isFound || (stripos($string, $value) !== false);
if ($isFound) break; // if a word was found, there is no need to continue
}
}
return $isFound;
}

这个函数返回 true 或 false ,如果单词找到它返回 1,如果没有找到它返回 0。我需要返回我正在搜索的单词,因为我想在 mysql 中再次搜索这个单词。就像如果函数找到“Ary digital”那么它应该返回“ary digital found”。需要帮助。谢谢。

最佳答案

您可能想做这样的事情(未经测试):

preg_match('#(word1|word2|word3)#',$string,$matches);

然后 print_r($matches) 查看 matches 数组的输出并获取您想要的位。从那里您可以返回 true/false 等。

关于php - 使用正则表达式查找字符串中的单词数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25907103/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com