求大神帮忙,帮忙实现 文章中内容中的特定关键词加链接

创建时间:2020-09-04 00:04发布者:哈哈哥点击数:1225
我已经创建了一张表关键词表
字段有,关键词,链接,
然而,我从网上找来的程序跟douphp 怎么都融合不了,
请求大神给帮忙看看是什么问题,
————————————————————————————

Class Keywords
{
function get_keywords_list() {
$sql = "SELECT * FROM " . $GLOBALS['dou']->table('keywords'). " ORDER BY displayorder ASC, id ASC";
$query = $GLOBALS['dou']->query($sql);
while ($row = $GLOBALS['dou']->fetch_array($query)) {
$keywords_list[] = array (
"id" => $row['id'],
"name" => $row['name'],
"url" => $row['url'],
"add_time" => $row['add_time'],
"displayorder" => $row['displayorder']
);
}

return $keywords_list;
}
}
function mixkeyword($contentsource) {
$keywordsdata = new Keywords();
$keywordslist = $keywordsdata->get_keywords_list();

$replacedarray = array();
$matchnum = 0;
if(count($keywordslist) > 0)
{
usort($keywordslist, 'id');
foreach($keywordslist as $keywords)
{
$match_name = str_replace('\\','\\\\', $keywords->name);
$match_name = str_replace(array('/','{','}','^','$','*','+','?','.','|','-','[',']'), array('\/','\{','\}','\^','\$','\*','\+','\?','\.','\|','\-','\[','\]'), $match_name);
if(preg_match_all("/(<a[^>]+>.*?<\/a>)|(<img[^>]+>)|<strong>.*?<\/strong>|<h[1-6]>.*?<\/h[1-6]>/i",$contentsource,$matches,PREG_SET_ORDER))
{
foreach($matches as $match)
{
$contentsource = str_replace($match[0],"<link>".$matchnum."</link>",$contentsource);
$replacedarray[$matchnum] = $match[0];
$matchnum++;
}
}
if($keywords->url != $cururl)
{
$patterns = "/(?<!<link>)$match_name(?!<\/link>)/i";
$replacements = '<a href="'.$keywords.url.'">'.$keywords->name.'</a>';
$contentsource = preg_replace($patterns,$replacements,$contentsource,1);
}
else
{
$patterns = "/(?<!<link>)$match_name(?!<\/link>)/i";
$replacements = '<strong>'.$keywords->name.'</strong>';
$contentsource = preg_replace($patterns,$replacements,$contentsource,1);
}
}
}
if(preg_match_all("/(?<=<link>)\d+(?=<\/link>)/",$contentsource,$matches,PREG_SET_ORDER))
{
foreach($matches as $match)
{
$contentsource = str_replace("<link>".$match[0]."</link>",$replacedarray[$match[0]],$contentsource);
}
}
return $contentsource;
}


/* 获取详细信息 */
$article = $dou->get_row('article', '*', "id = '$id'");
$article->content = mixkeyword($a$contentsource->content);








回答 我也遇到这个问题(0)
哈哈哥2020-09-04 12:05回答
各位大神给帮忙看看,怎么修改这个代码,
有用(0) 没用(0)