开发者

PHP - similar_text() function returns inaccurate results?

开发者 https://www.devze.com 2023-01-23 21:40 出处:网络
I\'m working on a searchengine, and to filter out all the bad results i compare the result string with the search query, and if the results exceeds 30% it\'s returned.

I'm working on a searchengine, and to filter out all the bad results i compare the result string with the search query, and if the results exceeds 30% it's returned.

I know that works, i've done it before and it's great. However, for some odd reason it appears to return some really weird results for me now. I've been playing around with it for a while now and i just can't figure it out!

while ($stmt->fetch()) {
 $tmp_procent = similar_text("banana", "banana");
 //if ($tmp_procent > 30) {
  $result[] = array('id' => $id,
        'category_id' => $category_id,
        'example' => html($example),
        'example_name' => html($example_name));
 //}
 $procent_arr[] = $tmp_procent;
}
return $procent_arr;

As you can see i've commented the IF statement out开发者_运维知识库, so right now it returns all results. But that's not really the point... As you can see, i'm comparing two exactly identical strings (banana), and then returning them.

This is what i get after printing out the result:

Array
(
    [0] => 5
    [1] => 5
    [2] => 5
)

Eh, what? That's not correct! Shouldn't it be 100 on all results?

Any ideas guys?

Thanks!

Oh and i spelled "procent" in Swedish, so it's not misspelled, in case you noticed that. The reason it returns 3 results is because it finds three matches in the database.


It's hard to understand what you were trying to do.

I wonder why it returned 5. 'banana' has 6 characters. I think you did some typo?

Refer to php documentation, the syntax is similar_text($str1, $str2, &$perc) Adding the last parameter does not affect on return value. The return value would be 6 for 'banana'. What the $perc is, it a reference to the result of formula in the core of the function

(similar_characters/average of characters*100)

This is what has the value of 100 in your case.

$procent_arr = []; // Good practice to define your array out of the loop
while ($stmt->fetch()) {
    $tmp_procent = similar_text('banana', 'banana', $procent);
    if ($tmp_procent > 30) {
        $result[] = [
            'id' => $id,
            'category_id' => $category_id,
            'example' => html($example),
            'example_name' => html($example_name)
        ];
    }
    $procent_arr [] = $procent;
}
return $procent_arr;

The return value would be:

[0]=>100
[1]=>100
[2]=>100
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号