php提取关键字中文分词
php中文网最新课程
每日17点准时技术干货分享
需求:做SEO的keywords时,需要从标题或者正文里提取关键字
https://github.com/feixuekeji/PHPAnalysis
下载后解压放到extend目录下(以tp5为例,其他目录也行)
2.封装
/*** @auther: xxf* Date: 2019/8/19* Time: 11:04*/namespace WordAnalysis;/*** 中文分词提取关键字*/class Analysis{/*** Notes:关键字提取* @auther: xxf* Date: 2019/8/19* Time: 11:09* @param string $content* @param int $num 获取数量* @return string*/public static function getKeywords($content = "",$num = 3) {if (empty ( $content )) {return '';}require_once 'phpanalysis.class.php';\PhpAnalysis::$loadInit = false;$pa = new \PhpAnalysis ( 'utf-8', 'utf-8', false );$pa->LoadDict ();$pa->SetSource ($content);$pa->StartAnalysis ( true );$tags = $pa->GetFinallyKeywords ($num); // 获取文章中的n个关键字return $tags;//返回关键字}}
3.使用
$data['seo']['keyword'] = Analysis::getKeywords($article_info['title']);
相关推荐:
▼请点击下方:“阅读原文”,在线查看!
