[英]Check if strings are x% equal
I want to compare three strings: 我想比较三个字符串:
"a text string" //target string
"a kind of similar text string"
"the cow jumps over the moon"
And set a percentage parameter that returns the results that are x% similar to the target. 并设置一个百分比参数,该参数返回与目标类似的x%结果。
$results = compare($array,$target,80)
$results // Returns an array with str 1,2, because they are at least 80 similar to the target.
Also, is a similar function possible in JavaScript or jQuery? 此外,在JavaScript或jQuery中可能有类似的功能吗?
In PHP, there is the function similar_text
. 在PHP中,有一个函数
similar_text
。 As for JavaScript, there's the PHP.js project , which re-implements PHP functions in JavaScript. 至于JavaScript,有PHP.js项目 ,它在JavaScript中重新实现PHP函数。 They have an implementation of
similar_text
you can use. 他们有一个你可以使用的
similar_text
的实现。
The JavaScript implementation doesn't support the percent parameter, it seems. 似乎JavaScript实现不支持百分比参数。
The function you're looking for is similar_text . 您正在寻找的功能是similar_text 。
It takes an optional 3rd parameter (passed by reference) where the percentage difference is placed. 它需要一个可选的第三个参数(通过引用传递),其中放置百分比差异。
In your situation, the following should do: 在您的情况下,以下应该做:
// Returns true if $str1 is at least $pct similar to $str2, otherwise false.
function compare($str1, $str2, $pct)
{
$p = 0;
similar_text($str1, $str2, $p);
return ($p >= $pct);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.