PHP实现给文章关键词加链接功能
PHP实现给网站指定关键词替换a链接功能
<?php
//PHP实现给文章关键词加链接功能
header("Content-Type:text/html;charset=utf-8");
$content = 'PHP数组,怎么找出最大值与关键字1最小值,和它们所对应的数组下标';
$keywordArray = array(
array('关键字1', 'https://www.xxx.com/url3/'),
array('关键字2', 'https://www.xxx.com/url1/')
);
//调用方法
$c = new Content($content, $keywordArray);
$newContent = $c->addKeywordLink();
print $newContent;
//相关函数
class Content {
/**
* 文本
*
* @var string
*/
protected $_content;
/**
* 关键词
*
* @var string
*/
protected $_keyword;
/**
* 构造方法
*
* @param string $content
* @param string $keyword
*/
public function __construct($content, $keyword) {
$this->setContent($content);
$this->setKeyword($keyword);
}
/**
* 给关键词加上链接
*
* @return string
*/
public function addKeywordLink() {
$content = $this->getContent();
$keywordArray = $this->sortKeywordArray($this->getKeyword());
$htmlTagArray = $this->getAllHtmlTag($content);
$noTagContentArray = $this->splitContentByHtmlTag($content);
/**
这边定义了一个临时数组
$tempReplaceArray[1]用来存储所有关键词的正则表达式
$tempReplaceArray[2]用来存储关键词的md5值
$tempReplaceArray[3]用来存储需要替换的带链接的关键词
*/
$tempReplaceArray = array();
$tempReplaceArray[1] = array();
$tempReplaceArray[2] = array();
$tempReplaceArray[3] = array();
foreach ($keywordArray as $keyword) {
$tempReplaceArray[1][] = '/' . preg_quote($keyword[0]) . '/i';
$tempReplaceArray[2][] = '{' . md5($keyword[0]) . '}';
$tempReplaceArray[3][] = '<a href="' . $keyword[1] . '" target="_blank">' . $keyword[0] . '</a>';
}
//循环替换每一段内容
foreach ($noTagContentArray as $key => $noTagIntro) {
//如果是空内容,则直接进行下一步
if (!trim($noTagIntro)) {
continue;
}
$noTagIntro = preg_replace($tempReplaceArray[1], $tempReplaceArray[2], $noTagIntro,1); //设置只替换一次
$noTagIntro = str_replace($tempReplaceArray[2], $tempReplaceArray[3], $noTagIntro);
$noTagContentArray[$key] = $noTagIntro;
}
//组合提取的html标签和处理后的内容
$finalContentArray = array();
$count = count($noTagContentArray);
for ($i = 0; $i < $count; $i++) {
$finalContentArray[] = $noTagContentArray[$i];
if (isset($htmlTagArray[$i])) {
$finalContentArray[] = $htmlTagArray[$i];
}
}
return implode('', $finalContentArray);
}
/**
* 对关键词进行排序,最长的排前面
* @param array $keywordArray
* @return array $keywordArray
*/
public function sortKeywordArray($keywordArray) {
usort($keywordArray, function($a, $b) {
$al = strlen($a[0]);
$bl = strlen($b[0]);
if ($al == $bl)
return 0;
return ($al > $bl) ? -1 : 1;
});
return $keywordArray;
}
/**
* 提取出所有html标签
*
* @param string $content
* @return array
*/
public function getAllHtmlTag($content) {
preg_match_all('/<\/?[a-zA-Z]+[^>]*>/', $content, $match);
if (isset($match[0])) {
$htmlTagArray = $match[0];
return $htmlTagArray;
}
return array();
}
/**
* 根据html标签切割内容
*
* @param string $content
* @return array
*/
public function splitContentByHtmlTag($content) {
$noTagContentArray = preg_split('/<\/?[a-zA-Z]+[^>]*>/', $content);
return $noTagContentArray;
}
/**
* 设置关键词
*
* @param string $keyword
*/
public function setKeyword($keyword) {
$this->_keyword = $keyword;
}
/**
* 获取关键词
*
* @return string
*/
public function getKeyword() {
return $this->_keyword;
}
/**
* 设置内容
*
* @param string $content
*/
public function setContent($content) {
$this->_content = $content;
}
/**
* 获取内容
*
* @return string
*/
public function getContent() {
return $this->_content;
}
} 本文属原创,转载请注明原文:https://www.zhimatong.com/jiaocheng/675.html
为保证教程的实用性及扩大知识面覆盖,如果您有相似问题而未解决,可联系在线客服免费技术支持。
内容有用



交流群
购物车