2013-12-10 66 views
0

我有两个字符串,如:比较两个字符串并计算两个词多少次匹配

$string1 = 'Hi how are you today'; 
$string2 = 'how' AND 'today'; 

$字符串1可以是任何东西,因为它得到它是通过一个循环的内容。

我希望能够以计数$字符串2这两个词有多少次出现在$字符串1。这两个词都必须以任何顺序存在。

这是我到目前为止。但它只检查是否存在“文本”。我怎样才能改变它来检查“text”和“text2”是否以任何顺序存在。

$sitesToCheck = array("http://mysitecom.com", "http://mysitetwocom.com")); 

foreach ($sitesToCheck as $site) { 
    $html = file_get_html($site); 
    foreach ($html->find('.table tr td.span8') as $element) { 
     $list .= $element->plaintext; 
     $item_count = count(explode('text', $list)); 
    } 
} 

感谢,

回答

1

下面是做到这一点的方法之一。 您可以使用PHP的array_intersect

$string1 = 'Hi how are you today'; 
$string2 = 'how today'; 
$arr1 = explode(" ",$string1); 
$arr2 = explode(" ",$string2); 
$result = array_intersect($arr1 , $arr2); //matched elements 
$num = count($result); //number of matches 
相关问题