Home >Backend Development >PHP Problem >How to implement recommendation function in php

How to implement recommendation function in php

(*-*)浩
(*-*)浩Original
2019-09-29 15:19:083010browse

Function used: int similar_text (string $first, string $second[, float $percent])

How to implement recommendation function in php

##Use similar_text 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 get a list of articles similar to the original articles. (Recommended learning: PHP video tutorial)

<?php
$demo_title= "简明现代魔法";
$demo_arr_title= array("简单易懂的现代魔法","简单明了的现代魔法","简明扼要的古代魔法","不简单的现代魔法","很难懂的现代魔法");
$new_array= getSimilar($demo_title,$demo_arr_title);
//print_r($new_array);
echo"与[$demo_title]最相关的前三个文章是:<br/>";
for($j=0; $j<=2; $j++)
{
echo($j+1).":".$new_array[$j]."<br/>";
}
//$title当前标题,$arrayTitle为需要查找的数组
functiongetSimilar($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);
}
arsort($arr_similar); //按照相似的字节数由高到低排序
reset($arr_similar); //将指针移到数组的第一单元
$index= 0;
foreach($arr_similaras$old_index=>$similar)
{
$new_title_array[$index] = $arr_title[$old_index];
$index++;
}
return$new_title_array;
}
?>

Program running results:

与[简明现代魔法]最相关的前三个文章是:
1:简单明了的现代魔法
2:简单易懂的现代魔法
3:简明扼要的古代魔法

The above is the detailed content of How to implement recommendation function 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