设为首页收藏本站

简体中文 繁體中文 English 日本語 Deutsch 한국 사람 بالعربية TÜRKÇE português คนไทย french

搜索
热搜: 活动 交友 discuz

坛子里有没有正则高人?求助啊 [复制链接]
查看:125 | 回复:11

65

主题

1424

回帖

3115

积分

论坛元老

积分
3115
发表于 2010-10-11 07:23:01 | 显示全部楼层 |阅读模式
求个PHP正则验证URL的帖子

我的网站文章中有些链接作了xxx,有的没有,我想用正则将全站没有加、标签的http开头的链接都加上链接应该怎么写正则呢?

对于下面这样的内容不作改动[ol]
  • [url=]http://xxx.xxx.com/xxx/xxx.xxx[/url][/ol]复制代码[ol]

  • [img][/img]
    [/ol]复制代码这样的http开头的内容加标签[ol]
  • http://xxx.xxx.com/xxx/xxx.xxx[/ol]复制代码给个字符串吧[ol]

  • [img][/img]
    Artist : Circle II Circle
  • Album : Consequence Of Power
  • Label : AFM
  • Genre : Metal
  • Street date : 2010-00-00
  • Quality : 217 kbps / 44.1kHz / Joint Stereo
  • Encoder : Lame 3.97 -V2 vbr-new
  • Size : 83.07 MB
  • Time : 50:40 min
  • Url : http://222.222.222/222.222
    1. Whispers In Vain 5:24
  • 2. Consequence Of Power 4:25
  • 3. Out Of Nowhere 4:10
  • 4. Remember 5:30
  • 5. Mirage 5:05
  • 6. Episodes Of Mania 5:09
  • 7. Redemption 5:31
  • 8. Take Back Yesterday 5:03
  • 9. Anathema 5:16
  • 10. Blood Of An Angel 5:07
    Circle II Circles 5th studio album, Consequence Of Power,
  • is clearly the ultimate masterpiece created by master
  • vocalist Zak Stevens (ex Savatage) and his band! Strong
  • hooks, heavy riffs and Zaks unique voice have all fused on
  • Consequence Of Power to exceed their past catalogue. Watch
  • out for your favorite album of 2010: Consequence Of Power
  • by Circle II Circle!


    http://333.333.333/333.333
    http://555.555.555/555.555[/ol]复制代码目标是给字符串里的http://222.222.222/222.222http://333.333.333/333.333加链接
    恳请高人给个正则吧
  • 112

    主题

    4757

    回帖

    9902

    积分

    论坛元老

    积分
    9902
    发表于 2010-10-11 12:00:14 | 显示全部楼层
    没完全理解~~~

    235

    主题

    4363

    回帖

    9527

    积分

    论坛元老

    积分
    9527
    发表于 2010-10-11 14:10:10 | 显示全部楼层
    什么意思?

    65

    主题

    1424

    回帖

    3115

    积分

    论坛元老

    积分
    3115
     楼主| 发表于 2010-10-11 14:29:15 | 显示全部楼层
    就是给没有加标签的URL加上标签

    993

    主题

    5160

    回帖

    1万

    积分

    论坛元老

    积分
    13415
    发表于 2010-10-11 14:38:19 | 显示全部楼层
    [ol]

  • [img][/img]
    Artist : Circle II Circle
  • Album : Consequence Of Power
  • Label : AFM
  • Genre : Metal
  • Street date : 2010-00-00
  • Quality : 217 kbps / 44.1kHz / Joint Stereo
  • Encoder : Lame 3.97 -V2 vbr-new
  • Size : 83.07 MB
  • Time : 50:40 min
  • Url : http://222.222.222/222.222
    1. Whispers In Vain 5:24
  • 2. Consequence Of Power 4:25
  • 3. Out Of Nowhere 4:10
  • 4. Remember 5:30
  • 5. Mirage 5:05
  • 6. Episodes Of Mania 5:09
  • 7. Redemption 5:31
  • 8. Take Back Yesterday 5:03
  • 9. Anathema 5:16
  • 10. Blood Of An Angel 5:07
    Circle II Circles 5th studio album, Consequence Of Power,
  • is clearly the ultimate masterpiece created by master
  • vocalist Zak Stevens (ex Savatage) and his band! Strong
  • hooks, heavy riffs and Zaks unique voice have all fused on
  • Consequence Of Power to exceed their past catalogue. Watch
  • out for your favorite album of 2010: Consequence Of Power
  • by Circle II Circle!


    http://333.333.333/333.333
    [url=]http://555.555.555/555.555[/url]
  • ';
  • function remove_http($matches){
  •         return str_replace('http://','##########',$matches[0]);
  • }
  • $str = preg_replace_callback("/]*href=['"]*http:\/\/[^]*>http:\/\/[^]*/i",'remove_http',$str);
  • $str = preg_replace_callback("/]*src=['"]*(http:\/\/)[^]*>/i",'remove_http',$str);
  • $str = preg_replace("/http:\/\/[^\r\n\s]*/i",'[url=]\0[/url]',$str);
  • $str = str_replace('##########','http://',$str);
  • echo $str;
  • ?>[/ol]复制代码求更简单的方法

    [ 本帖最后由 gdtv 于 2010-10-11 14:40 编辑 ]
  • 9

    主题

    151

    回帖

    347

    积分

    中级会员

    积分
    347
    发表于 2010-10-11 15:31:04 | 显示全部楼层
    PHP Example: Automatically link URL's inside text.

    $text = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '[url=]$1[/url]', $text);

    65

    主题

    1424

    回帖

    3115

    积分

    论坛元老

    积分
    3115
     楼主| 发表于 2010-10-11 15:42:35 | 显示全部楼层
    谢谢高人

    但是如果还能在完美点就好了,现在对于下面这种情况会出问题
    5555555555555

    65

    主题

    1424

    回帖

    3115

    积分

    论坛元老

    积分
    3115
     楼主| 发表于 2010-10-11 15:46:24 | 显示全部楼层
    原帖由 ninjai 于 2010-10-11 15:31 发表


    PHP Example: Automatically link URL's inside text.

    $text = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '$1', $text);

    这个没有考虑已经在img和a标签内情况

    65

    主题

    1424

    回帖

    3115

    积分

    论坛元老

    积分
    3115
     楼主| 发表于 2010-10-11 15:49:10 | 显示全部楼层
    [ol]
  • $str = preg_replace_callback("/]*href=['"]*http:\/\/[^]*>[^]*/i","remove_http",$str);[/ol]复制代码这样好像就OK 了
  • 0

    主题

    1

    回帖

    4

    积分

    新手上路

    积分
    4
    发表于 2010-10-11 15:49:41 | 显示全部楼层
    5楼的方法应该算一个变通的方法
    先替换标签内URL的http://
    在匹配替换http://的URL
    再将第一步被替换的还原

    不知道还有没有更好的方法,用(?!exp)这类?求正则帝,求一行正则代码实现的方法
    您需要登录后才可以回帖 登录 | 立即注册

    论坛客服/商务合作/投诉举报:2171544 (QQ)
    落伍者创建于2001/03/14,本站内容均为会员发表,并不代表落伍立场!
    拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论!
    落伍官方微信:2030286 邮箱:(djfsys@gmail.com|tech@im286.com)
    © 2001-2014

    浙公网安备 33060302000191号

    浙ICP备11034705号 BBS专项电子公告通信管[2010]226号

      落伍法律顾问: ITlaw-庄毅雄

    Archiver|手机版|Discuz! X

    GMT+8, 2026-6-25 06:07 , Processed in 0.019754 second(s), 3 queries , Gzip On, Redis On.

    返回顶部