Home > Article > Backend Development > How to implement similar search in php
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.
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),'utf-8'); // 提取每一段关键词的前2个字作为新的关键词,每个关键词进行一次查询,将查询结果加入数组 // 最后返回数组就是最终的查询结果 $b = mb_substr($a,0,2,'utf-8'); echo $b."<br/>"; } ?>
Preprocessing keywords
From 0 The first word of the starting keyword is truncated and used as a new preprocessing keyword.
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.
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!