- 内链锚文本效果
- 实现教程
执行成功后,刷新后台,查看系统基本参数-其它选项,最下面 根据自己的需求填写和勾选INSERT INTO `dede_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文件
找到
改成if($arr['type']=='htmltext' && $GLOBALS['cfg_keyword_replace']=='Y' && !empty($this->Fields['keywords']))
再找到if($arr['type']=='htmltext' && $GLOBALS['cfg_keyword_replace']=='Y')
把整个函数改成function ReplaceKeyword($kw,&$body)
{
中间代码略过...
}
继续找到function GetTags()
{
global $cfg_cmsurl;
$this->dsql->SetQuery("SELECT tid FROM `dede_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 `dede_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 `dede_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;
}