PHP关联链接常用代码

方法一:手动编辑添加
方法二:使用程序在输出内容时实现,又不影响后台内容的编辑。 下面是PHP自动文章内容添加关联链接方法和示例: Keylinks函数方法
参数一:要处理的内容
参数二:替换次数
返回结果:处理后的内容 <div class="codetitle"><a style="CURSOR: pointer" data="74567" class="copybut" id="copybut74567" onclick="doCopy('code74567')"> 代码如下:

<div class="codebody" id="code74567">
header("Content-Type:text/html;charset=utf-8"); //设置编码
$linkdatas=array(
array('前端开发','http:////www.jb51.cc'),
array('前端设计','//www.jb51.cc'),
array('Web前端',
array('前端博客',
);
echo "替换前
";
echo $str='Web前端开发 - 专注于网站前端设计与Web用户体验。前端开发,专注Web前端开发,关注Web用户体验,关注国内外最新最好的前端设计资源和前端开发技术的专业前端博客';
echo "
替换后
";
echo $str=keylinks($str,2);
/*
关联关键词替换
@param txt $string 原字符串
@param replacenum $int 替换次数
@return string 返回字符串
/
function keylinks($txt,$replacenum = '') {
global $linkdatas;
if ($linkdatas) {
$word = $replacement = array();
foreach ($linkdatas as $v) {
$word1[] = '/(?!(<a.?))' . preg_quote($v[0],'/') . '(?!.<\/a>)/s';
$word2[] = $v[0];
$replacement[] = '<a href="' . $v[1] . '" target="_blank" class="keylink">' . $v[0] . '';
}
if ($replacenum != '') {
$txt = preg_replace($word1,$replacement,$txt,$replacenum);
} else {
$txt = str_replace($word2,$txt);
}
}
return $txt;
}

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...