所以目前我有一个问题。我有这段代码来查看一个短语是否出现在另一个短语中:
if(strstr($matches[1], $query))例如,如果:
$matches[1] = "arctic white"
$query = "arctic"在上面的例子中,代码将检测到短语“北极”在短语“北极白”中,虽然我想让它检测它是否也在单词中,而不仅仅是短语。
例如,如果:
$matches[1] = "antarctica"
$query = "arctic"在这种情况下,脚本将不会检测到“南极洲”中的“北极”一词,尽管它是。所以我想知道,我如何编辑if(strstr($matches[1], $query)),以便它能检测到其中包含$query内容的所有单词?请帮帮我!
发布于 2012-06-10 03:23:51
您可以使用preg_match()来获得更好的结果。preg_match并不只包含正则表达式。它完全可以做你需要的事情。即:
if (preg_match("/arctic/i", "antarctica")) {
// it is there do something
} else {
// it is not there do something else
}顺便说一句,小"i“表示区分大小写,请查看PHP手册以获取更多示例:http://php.net/manual/en/function.preg-match.php
发布于 2012-06-10 03:18:24
使用strpos()
示例:
$word = "antarctica";
$find = "arctic";
$i = strpos($word, $find);
if($i === false)
{
echo "not found";
}
else
{
echo "found";
}https://stackoverflow.com/questions/10963797
复制相似问题