3

php简单实现分词搜索,模糊搜索,多关键词搜索,模糊查询

 3 years ago
source link: https://segmentfault.com/a/1190000040040064
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
<?php
header("Content-type:text/html;charset=utf-8");
 
$keywords = "什么是快乐星球?";
 
for ($i=0; $i <= mb_strlen($keywords); $i++) { 
    // 将关键词的第一个字分割掉
    $a = mb_substr($keywords,$i,mb_strlen($keywords),'utf-8');
    // 提取每一段关键词的前2个字作为新的关键词,每个关键词进行一次查询,将查询结果加入数组
    // 最后返回数组就是最终的查询结果
    $b = mb_substr($a,0,2,'utf-8');
    echo $b."<br/>";
}
?>

预处理关键词

从0开始关键词的第一个字截掉,作为新的预处理关键词。
image.png

然后,将每一个截掉第一个字的关键词进行截取前面2个字作为独立的关键词,即分词。
image.png

最后对每个关键词进行查询数据库,如果查询到结果就加入到一个数组中,没有结果就不加入数组,一直将所有关键词都查完,将结果输出即可。

Web:www.likeyuns.com
Author:TANKING
WeChat:sansure2016


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK