PHP-查找两个文本之间的匹配单词数量?

问题描述:

我想找到两个文本之间的相似单词的数量

I want to find number of similar words between two texts

示例

$str1=the cat is on the roof  
$str2=the mouse is on the roof

$ str1和$ str2中的

the,is,on,theroof 字相似

the,is,on,the,roof words are similar in $str1 and $str2

所以输出将是第5位百分比是86%

我正在尝试类似的text()函数,但是此函数无法按我想要的方式工作.

I am try similar_text() function but this function not work as which i want.

轻松分解它们,然后使用array_diff:

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);

@

上面的编辑代码以显示百分比.但是请记住,如果数组1和数组2的字数不相同,您可能会得到错误的结果.

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.