2012-06-09 86 views
0

所以目前我有一个问题。我有此代码段,看是否有句话出现在另一个短语:PHP strstr差异问题

if(strstr($matches[1], $query)) 

因此,例如,如果:

$matches[1] = "arctic white" 
$query = "arctic" 

在上述情况下,该代码将检测短语“北极“在”北极白“一词中,尽管我想要的是它能够检测它是否在单词中,而不仅仅是短语。

例如,如果:

$matches[1] = "antarctica" 
$query = "arctic" 

在这种情况下,脚本就无法检测到单词“极寒”中的“南极人”虽然是。所以我想知道,我该如何编辑if(strstr($matches[1], $query)),以便检测所有含有$查询内容的单词?请帮忙!

+0

它已经这样做:http://ideone.com/eKGgd。 –

+0

你应该使用'strpos()'来代替。 –

+0

它已经检测到很好。我猜你实际上是想找到包含$查询内容的单词? –

回答

2

可以使用的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

+0

他纠正了他的帖子。他想要的话,你应该告诉他如何收集比赛。 –

+0

但他只显示了一个数组键 - >值。他的意思是说钥匙会存放需要搜索的长文本吗?请澄清。 – Milan

0

使用strpos()

例如:

$word = "antarctica"; 
$find = "arctic"; 

$i = strpos($word, $find); 

if($i === false) 
{ 
echo "not found"; 
} 

else 
{ 
echo "found"; 
}