4

3つの文字列を比較したい:

"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でも同様の機能が可能ですか?

4

2 に答える 2

5

In PHP, there is the function similar_text. As for JavaScript, there's the PHP.js project, which re-implements PHP functions in JavaScript. They have an implementation of similar_text you can use.

The JavaScript implementation doesn't support the percent parameter, it seems.

于 2012-04-13T21:23:21.660 に答える
3

The function you're looking for is 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);
}
于 2012-04-13T21:23:38.147 に答える