a链接启动记事本_更改phpcms的内链链接为自定义

原phpcms的文章内自带了内链锚文本连接和javascript两种链接方式,为什么还需要修改做?

1、当前页面不能识别,也能链接到自己,SEO不友好,不行!

2、当前页面的链接,不同关键词链接到同一个url,SEO不友好,不行!

做为动手党,不行的地方自已动手,是一种享受,哈哈,有没有人跟我想法一样的?行动起来!GO!

首先,关闭掉content列的自带内链功能:

f0f55d2b73ffacb001c590e5abfa0e3c.png

713ebe0d3da245d8dea73caff39e325a.png

77904d0b039c5ec9ac40431ca1b91c6c.png

124f6751f1385ea49502ab5976f2a624.png

基本设置完成,在content模块下,修改index.php文件

在function show方法下搜索extarct函数,在其下面加入:

//自定义关联联连,请取消模型->内容里面自带的关联链接
$mem = new Memcache();
$mem->connect('127.0.0.1',11211);
$key = md5(APP_PATH.'__keylink__');
if(!($mem->get($key))){
	$keylinkDB =  pc_base::load_model('keylink_model');
	$keylinkRs = $keylinkDB->select('');
	$mem->add($key,serialize($keylinkRs),0,time()+3600);
}else{
	$keylinkRs = unserialize($mem->get($key));
}
$content = preg_replace_callback('/(<a[^>]+>)(.*?)(</a>)/is',function($matches){return $matches[1].base64_encode($matches[2]).$matches[3];},$content);
$search = "/(alts*=s*|titles*=s*)["|'](.+?)["|']/is";
$content = preg_replace_callback($search, array($this, '_base64_encode'), $content);
$word = array();
$urls = array();
$replacement = array();
foreach($keylinkRs as $value){
	if($value['url'] != $url){
			if(!in_array($value['url']) && stristr($content,$value['word']) && !in_array($value['url'],$urls)){
					$word[] = "/".$value['word']."/";
					$replacement[] = '<a href="'.$value['url'].'" target="_blank" class="keylink">'.$value['word'].'</a>';
					$urls[] = $value['url'];
			}
	}
}
if($word && $replacement){
	$content = preg_replace($word,$replacement,$content,1);
}
$content = preg_replace_callback('/(<a[^>]+>)(.*?)(</a>)/is',function($matches){
	if($matches[2] == base64_encode(base64_decode($matches[2]))){
			return $matches[1].base64_decode($matches[2]).$matches[3];
	}else{
			return $matches[1].$matches[2].$matches[3];
	}
},$content);
$content = preg_replace_callback($search, array($this, '_base64_decode'), $content);

在最后加入两个自定义函数:

function _base64_encode($matches) {
	return $matches[1].""".base64_encode($matches[2]).""";
}
function _base64_decode($matches) {
	return $matches[1].""".base64_decode($matches[2]).""";
}

这样,内容里面的内链就会被优化了,完全符合SEO标准的优化哦。这里注意,我使用了Memcache缓存,各位有需要的,也可以自已安装,windows下面安装稍微麻烦一点,如果是linux很容易,以CentOS来说,只需要执行:

yum -y install memcached php-pecl-memcache

然后启动即可:

/etc/rc.d/init.d/memcached start

----------------------------END------------------------

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值