问题
I want to find number of similar words between two texts
Example
$str1=the cat is on the roof
$str2=the mouse is on the roof
the,is,on,the,roof words are similar in $str1 and $str2
So output will be in number 5 OR In percentage 86%
I am try similar_text() function but this function not work as which i want.
回答1:
Easy, explode them and then use array_diff:
$totalWords = count($array_1);
$array_1 = explode(" ", $str1);
$array_2 = explode(" ", $str2);
$differenceCount = count(array_diff($array_1, $array_2));
$differentPercent = $differenceCount / ($totalWords / 100);
@Edit:
Edited code above to display percentage. However remember you may have a wrong result if the word count of array 1 and array 2 is not identical.
回答2:
$arr1 = explode(" ",$str1)
$arr2 = explode(" ",$str2)
$arr3 = array_diff($arr1, $arr2);
echo(count($arr1)-count($arr3));
回答3:
The way I would approach this is to explode each string and then use array_diff
to compare them like this:
$arr1 = explode(' ', $str1);
$arr2 = explode(' ', $str2);
$diff = array_diff($arr1, $arr2);
echo (count($arr1) - count($diff));
That will echo out the number of similar words.
回答4:
$arr1 = explode(" ",$str1)
$arr2 = explode(" ",$str2)
$arr3 = array_diff($arr1, $arr2);
i used array_intesect to check how many matches,, i used this in searching one array into other
and for preventing with auxilary verbs and prepositions(the,to,a,are etc) use -
$arr1 = str_replace(array("to", "the","a","an","in","by","but","are","is","had","have","has"),'',$arr1);
来源:https://stackoverflow.com/questions/21164358/php-finding-number-of-matching-words-between-two-pieces-of-text