我有两个字符串需要比较。假设字符串如下:
$A = "A fox jumped over the log";
$B = "A fox jumped";
现在我如何在 PHP 中比较这两个字符串以返回字符匹配的百分比。
所以我希望我的算法返回(对于上面的示例)
10/20 或 .5 因为 $B 正好有 $A 长度的一半匹配项。
我认为我可以迭代每个字符,但这似乎有点乏味,而且我的研究表明 preg_match 在这里对我没有多大帮助。感谢您的帮助!
I have two strings I need to compare. Let's say for example that the strings are as follows:
$A = "A fox jumped over the log";
$B = "A fox jumped";
Now how would I compare these two strings in PHP in order to return a percentage of character matching.
So I would like my algorithm to return (for the example above)
10/20 or .5 Since $B has exactly half of the matches of the length of $A.
I figured I could iterate through each character, but this seemed a bit tedious and my research indicated that preg_match wouldn't help me much here. Thanks for the help!
发布评论
评论(1)
尝试 levenshtein 或 similar_text 函数。
Try the levenshtein or similar_text functions.