V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
geo5078
V2EX  ›  PHP

php正则匹配url地址文章内容关键字自动添加链接 http://blog.ddian.cn/?post=989

  •  
  •   geo5078 · 2013-02-18 09:37:45 +08:00 · 3846 次点击
    这是一个创建于 4083 天前的主题,其中的信息可能已经有所发展或是发生改变。
    $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.****.com.cn".$nkeys[1]." target=_blank >".$nkeys[0]."</a>",$str);
    }
    //}
    }

    echo $str;



    方法二:自动给 URL添加链接


    Php代码
    <?php
    function autolink($foo)
    {
    $foo = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\1" target=_blank rel=nofollow>\1</a>', $foo);
    if( strpos($foo, "http") === FALSE ){
    $foo = eregi_replace('(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="http://\1" target=_blank rel=nofollow >\1</a>', $foo);
    }else{
    $foo = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\1<a href="http://\2" target=_blank rel=nofollow>\2</a>', $foo);
    }
    return $foo;
    }
    ?>

    调用这个函数来处理字符串如下:
    Php代码
    <?php
    $str = 'http://www.******.com';
    echo autolink($str);
    ?>
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1115 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 22:52 · PVG 06:52 · LAX 15:52 · JFK 18:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.