php实现相关文章推荐功能的方法:首先从文章列表中取出所有的文章标题;然后将所有的文章标题都同当前标题对比;接着将对比结果放入一个数组;最后利用similar_text函数获取与原文章相似的文章列表即可。
php怎么实现“相关文章推荐”功能?
PHP简单实现“相关文章推荐”功能的方法
摘要:通常在做内容网站的时候,需要在每一篇文章中出现与该文章相关的文章列表。对于大多数人来说,使用的方法通常是:建立一个关键词列表,判断每篇文章包含有那些关键词,最后根据关键词找出与某篇文章最相关的文章。对于内容比较复杂的网站,确定关键列表词显然会比较麻烦。
本文介绍了与以往方法不同的similar_text()函数来方便的达到我们的预期要求。
具体的思路是:从文章列表中取出所有的文章标题,将所有的文章标题都同当前标题对比,将对比结果生成一个数组,按照相似度的大小由大到标题,利用similar_text将这些文章标题同原文章标题做对比,按标题的相似程度重新排列标题,就得到了与原文章相似的文章列表。
这个思路用到的关键函数是:
int similar_text ( string $first, string $second[, float $percent] )
它返回的是两个字根串的相同字节数。
按照这个思路,我们建立如下的函数,这个函数的功能是把$arr_title数组按照同$title相似的的顺序重新排列数组。
<?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; } ?>
程序运行结果:
有些需要 注意的地方:
关于similar_text速度,有人做过这个一个测试,结果是:
The speed issues for similar_text seem to be only an issue for long sections of text (>20000 chars). I found a huge performance improvement in my application by just testing if the string to be tested was less than 20000 chars before calling similar_text. 20000+ took 3-5 secs to process, anything else (10000 and below) took a fraction of a second. Fortunately for me, there was only a handful of instances with >20000 chars which I couldn't get a comparison % for.
如果要直接使用正文作对比速度可能会比较慢。
据说这个函数用于英文的效果不太好(感兴趣的读者可以自行尝试)。用于英文时可以将英文句子用空格分开成多个单词后再写一个类似于similar_text的函数。
另外,如果句子中含有比较多“的”、“了”等非关键词字符时,得到的结果可能会不太理想。
更多相关知识,请访问PHP中文网!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
