网站文章自动增加站内链接这个我想大多数据同学的网站都有这个功能,下面小编总结了三种文章内链自动增加的功能,从上往下,最好的方法在最上面。
例1,代码如下:
- /**
- *对内容中的关键词添加链接
- *只处理第一次出现的关键词,对已有链接的关键不会再加链接,支持中英文
- *$content:string 原字符串
- *$keyword:string 关键词
- *$link:string,链接
- */
- function yang_keyword_link($content,$keyword,$link){
- //排除图片中的关键词
- $content = preg_replace( '|(<img[^>]*?)('.$keyword.')([^>]*?>)|U', '$1%&&&&&%$3', $content);
- $regEx = '/(?!((<.*?)|(<a.*?)))('.$keyword.')(?!(([^<>]*?)>)|([^>]*?</a>))/si';
- $url='<a href="'.$link.'">'.$keyword.'</a>';
- $content = preg_replace($regEx,$url,$content,1);
- //还原图片中的关键词
- $content=str_replace('%&&&&&%',$keyword,$content);
- return $content;
- }
例2,代码如下:
- include_once(dirname(__file__)."/../db/DBViewSpot.php" );
- class InnerLink{
- private static $spotUrlMap;
- /**
- * Generate view spots keywords link
- *
- * @param string $description
- * @param array $spotUrlMap
- * @return string
- */
- public static function genSpotLink($basePath, $description)
- {
- if(emptyempty(InnerLink::$spotUrlMap)){
- InnerLink::$spotUrlMap = DBViewSpot::getSpotPare();
- }
- // 排除不规则数据
- if ( emptyempty($description)) {
- return $description;
- }
- foreach (InnerLink::$spotUrlMap AS $spotUrlPair){
- $replace = "<a target='_blank' href='http://pzg412403.blog.163.com/blog/".$basePath."/".$spotUrlPair[1].".html'>".$spotUrlPair[0]."</a>";
- // 描述里面只有文字,没有图片,所以只要注意 a 链接
- $tmp1 = explode("<a",$description);
- $is_replaced=false;
- foreach ($tmp1 as $key=>$item){
- $tmp2 = explode("</a>",$item);
- if (sizeof($tmp2)>1) {
- if (substr($tmp2[0],0,1)!="a" && substr($tmp2[0],0,1)!="A"){
- if ($is_replaced===false) {
- $tmp2[1] = InnerLink::str_replace_once($spotUrlPair[0],$replace,$tmp2[1],$is_replaced);
- }
- $tmp1[$key] = implode("</a>",$tmp2);
- }
- }else {
- if (is_string($item) && $is_replaced===false) {
- $tmp1[$key] = InnerLink::str_replace_once($spotUrlPair[0],$replace,$item,$is_replaced);
- }
- }
- }
- $description = implode("<a",$tmp1);
- }
- return $description;
- }
- /**
- * replace key word for one time
- *
- * @param string $needle
- * @param string $replace
- * @param string $haystack
- * @param bool $is_replaced
- * @return string
- */
- private static function str_replace_once($needle, $replace, $haystack,&$is_replaced) {
- $pos = strpos($haystack, $needle);
- if ($pos === false) {
- return $haystack;
- }
- $is_replaced=true;
- return substr_replace($haystack, $replace, $pos, strlen($needle));
- }
- }
例3,这个是自己最初学php时写的,感觉有点问题,代码如下:
- <?php
- $keys =array(
- array('网页特效','/js_a/js.html'),
- array('seo','/seo/seo.html'),
- array('php','/phper/php.html'),
- array('jsp','/jsp/jsp.html'),
- array('asp','/asp/asp.html'),
- array('ps','/fw/photo.html'),
- array('photoshop','/fw/photo.html'),
- array('javascript','/js_a/js.html'),
- array('.net','/net/net.html'),
- array('非主流','/fw/photo.html'),
- array('网络','/mon/mon.html'),
- array('css','/cssdiv/css.html'),
- array('平面设计','/fw/photo.html'),
- array('网站','/person/'),
- array('网页制作','/wy/yw.html'),
- array('搜索引擎','/seo/seo.html'),
- array('优化','/seo/seo.html'),
- array('动画','/flash_a/flash.html'),
- array('数据库','/database/database.html'),
- array('挣钱','/mon/mon.html'),
- array('运营','/mon/mon.html')
- );
- $str ="今天是2010年5月30号,我的网站出现的问题这对seo有很多的问题,seo就是搜索引擎优化了,以前学php好啊现在觉得jsp好,css+div,网页,网页设计,网页制作,网页学习,网页教学,Photoshop,Flash,HTML,CSS,Dreamweaver,Fireworks,ASP,PHP,JSP,ASP.NET,网站建设,网站开发,网页特效,平面设计,个人网站,网页素材";
- echo $str,"<br>";
- foreach($keys as $nkeys){
- //print_r($nkeys);echo"<br>";
- //foreach( $nkeys as $join) {
- //echo($join),"<br>";
- if(strpos($str,$nkeys[0]) ){
- $str =str_replace($nkeys[0],"<a href=http://www.phpfensi.com".$nkeys[1]." target=_blank >".$nkeys[0]."</a>",$str);
- }
- //}
- }
- echo $str;
- ?>
波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
波比源码 » php实现文章内容关键字增加内链
波比源码 » php实现文章内容关键字增加内链
Thank you great post. Hello Administ . Hacklink Panel Hacklink
Thank you for great content. Hello Administ. Website: https://alanyaorjinalescort.com/