2013-12-10 68 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 
相關問題