从搜索的字符串PHP中删除常用单词

时间:2014-01-01 08:40:24

标签: php string comparison

好的,所以我有一串用空格分隔的单词,即

$ commonwords ='a,an,and,I,it,is,do,does,for,from,go,how,the etc';

我收到来自$ _POST ['searchquery']的搜索查询;

我想从搜索字符串中删除任何常用字词,我要查找的最终结果将是更简洁的关键字,然后我可以使用它来查询数据库。

因此,如果用户搜索“我如何搜索文件”

当删除所有常用词时,该字符串将成为“搜索文件”。

4 个答案:

答案 0 :(得分:4)

这样可行,但你必须添加大写的"如何"你的$ commonwords     

$commonwords = 'a,an,and,I,it,is,do,does,for,from,go,how,the,etc';

$commonwords = explode(",", $commonwords);


$search = "How do I search for files";

$search = explode(" ", $search);

foreach($search as $value){
    if(!in_array($value, $commonwords)){
        echo "$value<br/>";
        $query[] = $value;
    }
}   


    $query = implode(" ", $query);

    echo $query;
?>

答案 1 :(得分:2)

试试这个(CodePad):

<?php

$commonwords = 'a,an,and,I,it,is,do,does,for,from,go,how,the,etc';
$commonwords = explode(',', $commonwords);

$_POST['searchquery'] = "How do I search for files";
$_POST['searchquery'] = explode(' ', $_POST['searchquery']);
echo implode(' ', array_udiff($_POST['searchquery'], $commonwords, 'strcasecmp'));

它首先将两个字符串(您的列表和查询)分解为可操作的数组。然后它过滤差异并输出内爆阵列。

输出:

search files

答案 2 :(得分:2)

这是一个删除停用词的库:Remove Stop Words。您可以像这样使用它:

echo remove_stop_words('The quick brown fox jumps over the lazy dog');
// quick brown fox jumps   lazy dog

答案 3 :(得分:1)

为了获得更好的结果,将常用单词和搜索单词转换为小写或大写,这里我使用小写,

<?php
$commonwords = 'a,an,and,i,it,is,do,does,for,from,go,how,the,etc';

$commonwords = explode(",", $commonwords);


$search = strtolower("How do I search for files");

$search = explode(" ", $search);

foreach($search as $value){
    if(!in_array($value, $commonwords)){
       $query[] = $value;
    }
} 

    $query = implode(" ", $query);

    echo $query; // this is output
?>