首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP关键词搜索

PHP关键词搜索
EN

Stack Overflow用户
提问于 2011-12-08 14:29:28
回答 2查看 288关注 0票数 0

我一直在为网站上的某些类型的帖子构建PHP搜索工具(为此目的,请接受mySQL是不可能的)。

经过一系列的过程,我们得到标题和每个帖子的标记,并将它们存储在一个名为$full的变量中。

搜索项位于一个名为$terms的变量中。

代码语言:javascript
复制
$full = $title . ' ' . $tago[$result->ID];

两者都转换为小写。

然后,我们希望在$full中使用$terms查找类似的单词。

我试过这个。

代码语言:javascript
复制
$final = strpos($full,$terms);

它起作用了,但没有我所需要的那么好。

  • ,这将匹配标题和标签中类似的单词,但根本不处理空格。我试着从标题和标签中删除空格和逗号,但没有用。
  • ,如果用户输入由两个标签而不是一个标签组成的某个人的名字,它将找不到任何结果。
  • 不能处理多个单词,更不用说多个词了,这两个词我都想做。

这是一个完整的脚本,如果有任何帮助的话

代码语言:javascript
复制
$proto = $_GET['p'];
$terms = $_GET['s'];

$terms = strtolower($terms);
$terms = str_replace(' ', '', $terms);

$ids = array();

if($proto == 'inline') {

    $search = get_posts('post_type=post&post_status=publish');

    foreach($search as $result) {

        $title = get_the_title($result);

        $tags = wp_get_post_tags( $result->ID);

        foreach($tags as $tag){ $tago[$result->ID].= $tag->name;}

        $full = $title . ' ' . $tago[$result->ID];
        $full = strtolower($full);
        $final = strpos($full,$terms);


        if($final != false){ 

            $ids[] = $result->ID;

         }

    }
    if ($ids[0] == '') { 
        echo '<div align="center" style="text-align:center; color:#FFF;">No Results Found</div>';
    return false; } else {
    $args = array( 'post__in' => $ids );

    $srs = get_posts($args);

    foreach($srs as $sr) { 

    echo '<a href="'.$sr->post_slug.'"><img src=""/><b>'.$sr->post_title.'</b>'. $tago[$result->ID].'<span>'.date('dS M Y', strtotime($sr->post_date)).'</span></a>';

     }
    }


}

价值

$terms可能包含用户为搜索输入的一些值,比如“红色汽车”;

$full包含帖子标题和标签,可以这么说。红色的货车不是很好,车,车,可怕的,丑陋的

所以在这种情况下应该能找到。

EN

回答 2

Stack Overflow用户

发布于 2011-12-08 16:37:41

有几种方法你可以获得它,我将尝试提供一些:

STRPOS

这将匹配红色,然后停止,但它也将匹配非精确的词,例如,汽车也会匹配卡片等。

代码语言:javascript
复制
$words = explode(' ', $terms);

foreach ($words as $word) 
{
    if (false !== strpos()) {
        $ids[] = $result->ID;
    }
}

使用阵列Intersec的

代码语言:javascript
复制
//create an array of searched terms
$words = explode(' ', $terms);

//remove non letter numbers
$fullClean = preg_replace('/[^a-z\d\s]/', '', $full);

//Create an array of words
$criteria = explode(' ', $fullClean);

//find if any elements of $words exist in $criteria
if (count(array_intersect($words, $criteria))) {
    $ids[] = $result->ID;
}

第三种方法可能是使用正则表达式和preg_quote,但它很可能会遇到与strpos相同的问题。

希望这有帮助

票数 0
EN

Stack Overflow用户

发布于 2011-12-08 17:49:17

一个真正的搜索引擎所要做的就是建立一个倒排索引,也就是在最简单的形式下,一个从每个单词到包含这个词的文档集的查找表,以及有多少次。(文档仅指在上面搜索的文本)在php中很容易做到:

代码语言:javascript
复制
foreach($documents as $docIndex => $documentText) {
    //remove all types of punctuation and other characters here
    $documentText = str_replace(array(',','.','?','!'),"",$documentText);
    $words = explode(" ",$documentText);
    foreach($words as $word) $invertedIndex[$word][$docIndex]++;
}

运行后,我们已经建立了倒排索引。现在,要在您的示例中使用它,传入的查询是“红色汽车”。将其拆分,并查找$invertedIndex‘’red‘和$invertedIndex’‘car’,每个数组都将返回数组,这些数组中包含这些单词的所有文档以及它们的次数。要获得这两个数组的文档,请使用array_intersect获取文档,或者在这些数组的键上使用array_merge:

代码语言:javascript
复制
foreach($keywords as $count => $keyword) {
    if($count == 0) $validDocs = keys($invertedIndex[$keyword]);
    $validDocs = array_intersect(keys($invertedIndex[$keyword]),$validDocs);
}

现在,包含所有关键字的每个文档的文档索引都将在$validDocs中,如果您想按文本中单词出现的次数对它们进行排序,您也可以在$invertedIndex中获得该信息。这个方法非常快,但是你必须提前构建倒排索引,但是它比实际搜索要快得多。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8432539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档