Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 检查字符串是否x%相等_Php_Javascript_String_Comparison - Fatal编程技术网

Php 检查字符串是否x%相等

Php 检查字符串是否x%相等,php,javascript,string,comparison,Php,Javascript,String,Comparison,我想比较三个字符串: "a text string" //target string "a kind of similar text string" "the cow jumps over the moon" 并设置一个百分比参数,该参数返回与目标相似的x%结果 $results = compare($array,$target,80) $results // Returns an array with str 1,2, because they are at least 80 simila

我想比较三个字符串:

"a text string" //target string
"a kind of similar text string"
"the cow jumps over the moon"
并设置一个百分比参数,该参数返回与目标相似的x%结果

$results = compare($array,$target,80)

$results // Returns an array with str 1,2, because they are at least 80 similar to the target.

另外,在JavaScript或jQuery中也可以使用类似的函数吗?

在PHP中,有这个函数。至于JavaScript,还有一个,它在JavaScript中重新实现PHP函数。他们有一个你可以使用的实现


JavaScript实现似乎不支持percent参数。

您正在寻找的函数是

它接受一个可选的第三个参数(通过引用传递),其中放置了百分比差异

在您的情况下,应采取以下措施:

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

+1-原因与我在下面的评论中的原因相同。@KristerAndersson:
compare
实际上不是一个PHP函数。@Rocket-不,我的意思是
类似的文本
,尽管我在给你写评论,但还是有点困惑。不管怎样,这个问题很好,所以+1。我从来没有使用过它,但我不得不使用
levenshtein()
。这两者恰好是密切相关的。感谢
levenshtein()
,我需要决定是使用那个还是另一个。两者都很好。在Javascirpt中有可能吗?@Derek:任何事情都有可能。我看得不多,但你也许可以在那里找到一个实现。