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()

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

报告相同问题?

悬赏问题

  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答