Dedehtml.com

织梦关键词内链支持TAG标签和文档关键词

摘要:文章内容自定义锚文本(支持系统TAG标签和自定义关键词链接),可选择性开启相关功能。

内链锚文本效果

实现教程

1、后台-系统-SQL命令行工具,执行

INSERT INTO `#@__sysconfig` (`aid`, `varname`, `info`, `groupid`, `type`, `value`) VALUES ('745', 'cfg_replace_links', '文档内链允许最大链接数', '7', 'number', '10'), ('746', 'cfg_replace_key', '是否用关键词做内链', '7', 'bool', 'Y'), ('747', 'cfg_replace_tag', '是否用TAG标签做内链', '7', 'bool', 'Y');

执行成功后,刷新后台,查看系统基本参数-其它选项,最下面

根据自己的需求填写和勾选

2、修改 /include/arc.archives.class.php

修改或覆盖之前可先备份这个文件,防止出错

伸手党

可以直接下载我修改好的这个文件,解压覆盖进你网站include文件夹里即可

这里只提供了utf8编码文件,gbk编码用户自行转换

下载

手动党

可以跟着下面来修改

打开 /include/arc.archives.class.php 找到,大概在231或者233行

if($arr['type']=='htmltext' && $GLOBALS['cfg_keyword_replace']=='Y' && !empty($this->Fields['keywords']))

改成

if($arr['type']=='htmltext' && $GLOBALS['cfg_keyword_replace']=='Y')

改过的可略过这一步

继续找到,大概在1187或者1189行

function ReplaceKeyword($kw,&$body)
{
	中间代码略过...
}

把整个函数改成

function GetTags()
{
	global $cfg_cmsurl;
	$this->dsql->SetQuery("SELECT tid FROM `#@__taglist` WHERE aid = '{$this->Fields['aid']}' ");
	$this->dsql->Execute();
	$ids = '';
	while ($row = $this->dsql->GetArray())
	{
		$ids .= ($ids == '' ? $row['tid'] : ',' . $row['tid']);
	}
	if ($ids != '')
	{
		$addsql = " WHERE id IN($ids) ";
	}
	$query = "SELECT * FROM `#@__tagindex` $addsql ORDER BY addtime DESC";
	$this->dsql->SetQuery($query);
	$this->dsql->Execute();
	$result = array();
	while ($row = $this->dsql->GetArray())
	{
		$result[trim($row['tag'])] = $cfg_cmsurl . "/tags.php?/" . urlencode($row['tag']) . "/";
	}
	return $result;
}

function GetKeyWord()
{
	$query = "SELECT * FROM `#@__keywords` WHERE `rpurl` <> '' ORDER BY `rank` DESC ";
	$this->dsql->SetQuery($query);
	$this->dsql->Execute();
	$result = array();
	while ($row = $this->dsql->GetArray())
	{
		$result[trim($row['keyword'])] = trim($row['rpurl']);
	}
	return $result;
}

function ReplaceRes($text, $key, $url)
{
	global $cfg_replace_num;
	$tmp  = $text;
	$tags = $a = array();
	if (preg_match_all("#<a[^>]+>[^<]*</a[^>]*>#su", $tmp, $m))
	{
		$a = $m[0];
		foreach ($m[0] as $k => $z)
		{
			$z   = preg_replace("#\##s", "\#", $z);
			$tmp = preg_replace('#' . $z . '#s', "[_a" . $k . "_]", $tmp, 1);
		}
	};
	if (preg_match_all("#<[^>]+>#s", $tmp, $m))
	{
		$tags = $m[0];
		foreach ($m[0] as $k => $z)
		{
			$z   = preg_replace("#\##s", "\#", $z);
			$tmp = preg_replace('#' . $z . '#s', "[_tag" . $k . "_]", $tmp, 1);
		}
	}
	$key1 = preg_replace("#([\#\(\)\[\]\*])#s", "\\\\$1", $key);
	$tmp  = preg_replace("#(?!\[_s|\[_a|\[_|\[_t|\[_ta|\[_tag)" . $key1 . "(?!ag\d+_\]|g\d+_\]|\d+_\]|s\d+_\]|_\])#us", '<a href="' . $url . '" target="_blank"><u>' . $key . '</u></a>', $tmp, $cfg_replace_num);
	if (!empty($a))
	{
		foreach ($a as $n => $at)
		{
			$tmp = str_replace("[_a" . $n . "_]", $at, $tmp);
		}
	}
	if (!empty($tags))
	{
		foreach ($tags as $n => $at)
		{
			$tmp = str_replace("[_tag" . $n . "_]", $at, $tmp);
		}
	}
	return $tmp;
}

function ReplaceKeyword($kw, &$body)
{
	global $cfg_replace_links, $cfg_replace_key, $cfg_replace_tag;
	$cfg_replace_links = isset($cfg_replace_links) ? $cfg_replace_links : 10; // 文档内链允许最大链接数
	$cfg_replace_key   = isset($cfg_replace_key) ? $cfg_replace_key : 'Y'; // 关键词内链(默认开启)
	$cfg_replace_tag   = isset($cfg_replace_tag) ? $cfg_replace_tag : 'N'; // TAG内链(默认不开启)

	if ($cfg_replace_key == 'Y' && $cfg_replace_tag == 'N')
	{
		$arrkey = $this->GetKeyWord();
	}
	elseif ($cfg_replace_key == 'N' && $cfg_replace_tag == 'Y')
	{
		$arrkey = $this->GetTags();
	}
	elseif ($cfg_replace_key == 'Y' && $cfg_replace_tag == 'Y')
	{
		$arrkey = $this->GetKeyWord() + $this->GetTags();
	}
	$keys = array();
	foreach ($arrkey as $k => $url)
	{
		$k = trim($k);
		if (!$k)
		{
			continue;
		}
		if (strpos($body, $k) !== false)
		{
			$keys[$k] = $url;
		}
	}
	$keys_tmp = array_keys($keys);
	usort($keys_tmp, "cmp");
	foreach ($keys_tmp as $i => $key)
	{
		$ki = $i + 2;
		if ($ki > $cfg_replace_links)
		{
			break;
		}
		if (is_array($keys[$key]))
		{
			$url = $keys[$key][rand(0, count($keys[$key]) - 1)];
		}
		else
		{
			$url = $keys[$key];
		}
		$body = $this->ReplaceRes($body, $key, $url);
	}
	return $body;
}

继续找到

function _highlight

在它的上面加入

function cmp($a, $b)
{
    if (mb_strlen($a) == mb_strlen($b))
    {
        return 0;
    }
    return (mb_strlen($a) < mb_strlen($b)) ? 1 : -1;
}

如图

完成

版权声明:本文为博主原创文章,未经博主允许不得转载。https://www.dedehtml.com/notes/dede-replacekeyword.html
(5)
打赏 微信扫一扫 微信 支付宝 QQ 扫码打赏