PHP实现给文章关键词加链接功能解决关键字相互包含问题
<?php class HotLink { protected $_content; protected $_keywords; protected $weburl; protected $locurl; public function __construct($content,$keywords,$weburl,$locurl) { $this->weburl=$weburl; $this->locurl=$locurl; $this->setContent($content); $this->setKeywords($keywords); } /** * 加链接 */ public function addHotLink(){ $content=$this->getContent(); // $kWordArr=$this->keyWordSort($this->getKeywords()); $htmlTagArr=$this->getAllHtmlTag($content); $noHtmlContentArr=$this->splitContentByTag($content); $tempReplaceHtmlTag='{'.md5('lvdora-news').'}'; $tempReplaceArray = array(); $temContent=implode($tempReplaceHtmlTag,$noHtmlContentArr); foreach ($this->getKeywords() as $key=>$kWords){ if($kWords[1]==$this->locurl){ continue; } if(stripos($kWords[1],'http')===false){ $kwd_url=$this->weburl.$kWords[1]; }else{ $kwd_url=$kWords[1]; } $kwd=$kWords[0]; // 从长至短替换关键字为链接,替换的同时查找有没有包含其他关键字,如果有,把其中子关键字替换成{子关键字的md5值} for($j=$key+1; $j<count($this->_keywords); $j++) { $subKwd = $this->_keywords[$j][0]; if(!$subKwd)continue; if(strpos($kwd, $subKwd) !== false) { $tmpKwd = md5($subKwd); $kwd = str_replace($subKwd, $tmpKwd, $kwd); $tmpKwds[$tmpKwd] = $subKwd; } } $tempReplaceArray[$key][1]='/' . preg_quote($kWords[0]) . '/i'; $tempReplaceArray[$key][2]='<a href="' .$kwd_url. '" target="_blank" style="color:blue">' . $kwd . '</a>'; $tempReplaceArray[$key][3]='{' . md5($kWords[0]) . '}'; $tempReplaceArray[$key][4]=$kWords[2]; $temContent=preg_replace($tempReplaceArray[$key][1],$tempReplaceArray[$key][3],$temContent,$tempReplaceArray[$key][4]); $temContent=str_replace($tempReplaceArray[$key][3],$tempReplaceArray[$key][2],$temContent); } //把{子关键字的md5值}替换回来 foreach($tmpKwds as $tmp=>$kwd) { $temContent = str_replace($tmp, $kwd, $temContent); } $noHtmlContentArr=explode($tempReplaceHtmlTag,$temContent); $result=''; foreach ($noHtmlContentArr as $key=>$val){ if ($key==(count($noHtmlContentArr)-1)){ $result.=""; }else{ $result.=$val.$htmlTagArr[$key]; } } return $result; } /** * 根据html标签切割内容 * * @param string $content * @return array */ public function splitContentByTag($content) { return preg_split('/<a[^>]*>.*?<\/a>|<\/?[a-zA-Z]+[^>]*>/', $content); } /** * 提取出所有html标签 * * @param string $content * @return array */ public function getAllHtmlTag($content) { preg_match_all('/<a[^>]*>.*?<\/a>|<\/?[a-zA-Z]+[^>]*>/', $content, $match); if (isset($match[0])) { $htmlTagArray = $match[0]; return $htmlTagArray; } return array(); } /** * 对关键词进行排序,最长的排前面 * @param array $keyWordsArr * @return array $keyWordsArr */ protected function keyWordSort($keyWordsArr) { usort($keyWordsArr, function($a, $b) { $al = strlen($a[0]); $bl = strlen($b[0]); if ($al == $bl) return 0; return ($al > $bl) ? -1 : 1; }); return $keyWordsArr; } /** * 设置内容 * @param $content 内容 */ protected function setContent($content){ $this->_content=$content; } /** * 获取内容 * @return mixed */ protected function getContent(){ return $this->_content; } /** * 设置关键词 * @param $keyWords 关键词 */ protected function setKeywords($keyWords){ $this->_keywords=$keyWords; } /** * 获取关键词 * @return mixed */ protected function getKeywords(){ return $this->_keywords; } } $relatedlink_data = array ( 0 => array ( 0 => '巨蟹座的运势', 1 => 'https://www.jiyunwang.cn/index.php', 2 => 1, ), 1 => array ( 0 => '巨蟹座', 1 => 'xingzuo/juxie/', 2 => 1, ) ); $hotLink = new HotLink('<p> vvv巨蟹座的运势bbb背景人巨蟹座巨蟹座巨蟹座<br/> </p>', $relatedlink_data, 'www.baidu.com', 'show-' . 12 . '.html'); $a = $hotLink->addHotLink(); var_dump($a);
参考:https://blog.csdn.net/johnhan9/article/details/88635093
如果本文对你有帮助,欢迎打赏本站