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

Me7*_*888 0 php match fuzzy-logic levenshtein-distance

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

$str1=the cat is on the roof  
$str2=the mouse is on the roof
Run Code Online (Sandbox Code Playgroud)

在$ str1和$ str2中,the,on,on, words字是相似的

因此输出数量为5或者百分比为86%

我尝试使用similar_text()函数,但这个函数不能正常工作.

Ste*_*ini 6

容易,爆炸它们然后使用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);
Run Code Online (Sandbox Code Playgroud)

@编辑:

上面编辑的代码显示百分比.但是请记住,如果数组1和数组2的字数不相同,则结果可能不正确.