Home  >  Article  >  Backend Development  >  How to implement similar search in php

How to implement similar search in php

藏色散人
藏色散人Original
2022-01-21 10:19:251678browse

php method to implement similar search: 1. Create a PHP sample file; 2. Split the first word of the keyword; 3. Extract the first 2 words of each keyword as a new key word; 4. Query each keyword once and add the query results to the array; 5. Return the array.

How to implement similar search in php

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

How to achieve similar search in php?

php simply implements word segmentation search, fuzzy search, multi-keyword search, fuzzy query

<?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),&#39;utf-8&#39;);
    // 提取每一段关键词的前2个字作为新的关键词,每个关键词进行一次查询,将查询结果加入数组
    // 最后返回数组就是最终的查询结果
    $b = mb_substr($a,0,2,&#39;utf-8&#39;);
    echo $b."<br/>";
}
?>

Preprocessing keywords

From 0 The first word of the starting keyword is truncated and used as a new preprocessing keyword.

How to implement similar search in php

Word Segmentation

Then, for each keyword with the first word cut off, the first two words are cut out as independent The keyword, that is, the participle.

How to implement similar search in php

Finally, query the database for each keyword. If the result is found, it will be added to an array. If there is no result, it will not be added to the array. All keywords will be checked all the time. When finished, just output the result.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to implement similar search in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn