21十二/114
在dede文章中添加锚文本
在上一次在文章中添加关键词的基础上,终于把文章锚文本的事情搞定了,在此记录一下
首页在文章模板中的,把body标签
1 | {dede:field.body/} |
用下面这段代码替换
1 | {dede:field name='id' function='into_key(@me)'/} |
然后在includecommon.func.php文件中添加into_key等相关函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | /*增加关键词*/ function into_key($actileid){ //获得文章内容 $dsql = new DedeSql(false); $row = $dsql->GetOne("SELECT * FROM `#@__addonarticle` WHERE `aid` = {$actileid} LIMIT 0, 1"); $actile = $row['body']; //判断是否存在关键词 $keywords=array("扎金花","炸金花","诈金花","扎金花游戏大厅","同城游戏大厅下载","梭哈","梭哈游戏","梭哈游戏下载","58w同城游"); $keyword=" <a href='http://www.58w.cn/'>"."<b>".$keywords[mt_rand(0,count($keywords)-1)]."</b>"."</a> <span>www.58w.cn<span>"; foreach ($keywords as $k => $v){ $is_word=''; $keyw=''; $keyw="<b>".$v."</b>"; if(strstr($actile,$keyw)){ $is_word=1; continue; }else $is_word=0; } //判断是否存在url $keyurls=array("www.58w.cn","www.sky58.com","www.yes58.net/58w","www.pk28.com"); $keyurl=" <font style='color:#fff'>".$keyurls[mt_rand(0,count($keyurls)-1)]."</font>"; if(strstr($actile,'<span>www.58w.cn<span>'))$is_url=1; else $is_url=0; //分离文章 $str=preg_replace('/<p(.*)>/isU','',$actile); $arr=explode('</p>',$str); $arr=array_remove_empty($arr); //随机加粗文章段落 $txtnum=mt_rand(0,count($arr)-1); $arr[$txtnum]='<b>'.$arr[$txtnum].'</b>'; //加入关键词 if($is_word==0){ $key=count($arr)-1; $arr[$key]=$arr[$key].$keyword; } //加入url if($is_url==0){ $urlnume=mt_rand(0,count($arr)-1); $arr[$urlnume]=$arr[$urlnume].$keyurl; } //合并文章 foreach ($arr as $k => $v){ $arr[$k]='<p>'.$arr[$k].'</p>'; } $actile=implode('',$arr); //添加锚文本,以tag为准 $actile=txt_url($actile,$row['aid']); //返回文章 return $actile; } function txt_url($actile,$aid){ //得到文章tags,以tid排序 $dsql = new DedeSql(false); $query = "SELECT * FROM `#@__taglist` WHERE aid='$aid' order by tid asc"; $dsql->Execute('tag',$query); $i=0; while($row = $dsql->GetArray('tag')) { $i++; $tags[$i]['tag'] = $row['tag']; $tags[$i]['tid'] = $row['tid']; }//print_r($tags);echo '<hr>'; //得到相关tags随机文章id,先搜索tid靠前的tag相关文章 $j=0; foreach ($tags as $k => $v) { $query = "SELECT * FROM `#@__taglist` WHERE `tid` = '{$v['tid']}' and aid != {$aid}"; $dsql->SetQuery($query); $dsql->Execute(); $arraid=array(); while($row = $dsql->GetArray()) { $arraid[] = $row['aid']; } if(empty($arraid))continue; else{ $j++; shuffle($arraid); $newtag[$j]['aid']=$arraid['0']; $newtag[$j]['tag']=$v['tag']; continue; } }//print_r($newtag);echo '<hr>'; //得到相应文章的url访问规则和文章标题 foreach ($newtag as $k => $v){ $row = $dsql->GetOne("Select arc.title as title,t.typedir as dir from `#@__archives` arc left join #@__arctype t on arc.typeid=t.id where arc.id={$v['aid']}"); $urldir = $row['dir']; $urltitle = $row['title']; $row['dir']=str_replace('{cmspath}','http://www.58w.cn',$row['dir']); $trueurl=$row['dir'].'/'.$v['aid'].'.html'; $newtag[$k]['link']="<a href='{$trueurl}' title='{$urltitle}'>".$v['tag']."</a>"; } //print_r($newtag);echo '<hr>'; //提取超链接,图片等html标签,以免将超链接里面title或alt含有的关键词替换掉 $pat1 = '/<a(.*)<\/a>/isU'; $pat2 = '/<img(.*)>/isU'; $pat3 = '/<font(.*)<\/font>/isU'; $alink1=get_html($pat1,$actile); $alink2=get_html($pat2,$actile); $alink3=get_html($pat3,$actile); $alink=array_merge($alink1,$alink2,$alink3); foreach ($alink as $k => $v){ $actile=str_replace($v,'/*/'.$k.'/*/',$actile); } //用锚文本替换关键词,只替换一次,只替换一个关键词 foreach ($newtag as $k => $v){ if(!strstr($actile,$v['tag']))continue; else { $actile=preg_replace("/{$v['tag']}/","{$v['link']}",$actile,'1'); break; } } //print_r($alink);echo '<hr>'; //将超链接,图片等还原 foreach ($alink as $k => $v){ $actile=str_replace('/*/'.$k.'/*/',$v,$actile); } return $actile; } /*提取超链接,图片等html标签*/ function get_html($pat,$actile){ preg_match_all($pat, $actile, $match); return $match['0']; } /*去除空数组*/ function array_remove_empty(&$arr, $trim = true) { foreach ($arr as $key => $value) { if (is_array($value)) { array_remove_empty($arr[$key]); } else { $value = trim($value); $value = str_replace(' ','',$value); if ($value == '') { unset($arr[$key]); } elseif ($trim) { $arr[$key] = $value; } } } $arr=array_values($arr); return $arr; } |
这样就实现在文章段落末尾随机添加隐藏网址,文章末尾随机关键词,以及锚文本等功能了
2012年04月11日 02:41
Thanks for all of your labor on this website. Betty really loves conducting research and it’s simple to grasp why. Almost all notice all about the lively medium you provide informative suggestions on your blog and increase response from the others about this theme then our favorite girl is truly being taught a lot. Take advantage of the rest of the new year. You’re conducting a very good job.
2012年05月17日 03:52
What liberating knowledge. Give me liebtry or give me death.
2012年05月18日 15:55
Times are changing for the better if I can get this olnnie!
2012年04月11日 21:34
Thank you so much for giving everyone a very remarkable possiblity to check tips from this website. It really is so amazing plus packed with fun for me personally and my office colleagues to visit your site at the least 3 times per week to learn the fresh things you will have. And lastly, I’m always satisfied with your astounding opinions you give. Certain 2 points in this article are unequivocally the most effective we have all had.