duanjun7801 2016-09-29 18:16
浏览 25
已采纳

从大型数组中搜索php字符串以进行页面过滤

Basically I have a huge array of possibilities that could be for example sports teams or sport names:

Toronto Maple Leafs New Jersey Devils Boston Red Socks Hockey Soccer etc...

So I have a search bar where the user can type in anything they want.. I need a way to take what they enter in compare it to the array and if it is a close enough match add it to a filter variable.

example:

if (strpos($userSearch, 'Hockey') !== false) {
    $pageVar = $pageVar . "+" . "Hockey";
}

Doing it this way ^ has some set backs one lets say someone enters hockie or something like that.. or Toronto instead of Toronto maple leafs.. without going through all the possible cases one by one there must be a better way..

Thanks

  • 写回答

1条回答 默认 最新

  • douyuan3842 2016-09-29 19:08
    关注

    For an exact match, you can use in_array()

    $input = 'carrrot';
    $words  = array('apple','pineapple','banana','orange','radish','carrot','pea','bean','potato');    
    if (in_array($words, $input)) {
        echo "$input was found in array
    ";
    }
    

    For similar match, you can try levenshtein() (first example on php doc page)

    $input = 'carrrot';
    $words  = array('apple','pineapple','banana','orange','radish','carrot','pea','bean','potato');
    $shortest = -1;
    foreach ($words as $word) {
        $lev = levenshtein($input, $word);
        if ($lev == 0) {
            $closest = $word;
            $shortest = 0;
            break;
        }
        if ($lev <= $shortest || $shortest < 0) {
            $closest  = $word;
            $shortest = $lev;
        }
    }
    echo "Input word: $input
    ";
    if ($shortest == 0) {
        echo "Exact match found: $closest
    ";
    } else {
        echo "Did you mean: $closest?
    ";
    }
    

    Result:

    Input word: carrrot
    Did you mean: carrot?
    

    also for similar match, you can try similar_text()

    $input  = 'iApple';
    $words = array('apple','pineapple','banana','orange','radish','carrot','pea','bean','potato');
    $shortest = 70;
    foreach ($words as $word) {
        similar_text($word, $input, $percent);   
        $percent = round($percent);
        if ($percent == 100) {
            $closest = $word;
            $shortest = 100;
            break;
        }
        if ($percent >= $shortest) {
            $closest  = $word;
            $shortest = $percent;
        }  
    }
    echo "Input word: $input
    ";
    if ($shortest == 100) {
        echo "Exact match found: $closest
    ";
    } else {
        echo "Did you mean: $closest?
    ";
    }
    

    Result:

    Input word: iApple
    Did you mean: apple?
    

    To achieve good results you can use a combination of levenshtein(), similar_text(), and soundex()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭