Home >Backend Development >PHP Problem >How to simply implement the 'related article recommendation' function in PHP
Usually when building a content website, a list of articles related to the article needs to appear in each article. For most people, the method usually used is to build a keyword list, determine which keywords each article contains, and finally find the articles most relevant to a certain article based on the keywords. The editor below will show you how to implement it.
Usually when building a content website, a list of articles related to the article needs to appear in each article. For most people, the method usually used is: build a keyword list, determine which keywords each article contains, and finally find the article most relevant to an article based on the keywords. For websites with more complex content, it will obviously be more troublesome to determine the keyword list words.
Specific ideas: Take out all article titles from the article list, compare all article titles with the current title, generate an array from the comparison results, and use similar_text to Compare the titles of these articles with the titles of the original articles, rearrange the titles according to the degree of similarity of the titles, and obtain a list of articles similar to the original articles.
Key function:
<?php int similar_text ( string $first, string $second[, float $percent] ) ?>
It returns the same number of bytes of the two root strings.
<?php /** * 获取相关 * @param [type] $title 当前标题 * @param [type] $arr_title 需要查找的数组 * @return [type] array() 按照相关程度与$title最相关的数组 */ public function getSimilar($title,$arr_title){ $arr_len = count($arr_title); for($i = 0;$i <= ($arr_len -1 ) ;$i++ ){ //获取两个字符串相似的字节数 $arr_similar[$i] = similar_text($arr_title[$i], $title , $persent); } arsort($arr_similar); //按照相似的字节数由高到低排序 reset($arr_similar); //将指针移到数组的第一单元 $index = 0; foreach($arr_similar as $old_index=>$similar){ $new_title_array[$index] = $arr_title[$old_index]; $index++; } return$new_title_array; }
Recommended learning: php video tutorial
The above is the detailed content of How to simply implement the 'related article recommendation' function in PHP. For more information, please follow other related articles on the PHP Chinese website!