全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 3109|回复: 12

坛子里有没有正则高人?求助啊

[复制链接]
发表于 2010-10-11 07:23:01 | 显示全部楼层 |阅读模式
求个PHP正则验证URL的帖子

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

对于下面这样的内容不作改动
  1. <a href="http://xxx.xxx.com/xxx/xxx.xxx">http://xxx.xxx.com/xxx/xxx.xxx</a>
复制代码
  1. <img src="http://xxx.xxx.com/xxx/xxx.xxx">
复制代码
这样的http开头的内容加<a>标签
  1. http://xxx.xxx.com/xxx/xxx.xxx
复制代码
给个字符串吧
  1. <p align="center"><img alt="1111" src="http://111.111.111/111.111" /></p><p>Artist : Circle II Circle
  2. Album : Consequence Of Power
  3. Label : AFM
  4. Genre : Metal
  5. Street date : 2010-00-00
  6. Quality : 217 kbps / 44.1kHz / Joint Stereo
  7. Encoder : Lame 3.97 -V2 vbr-new
  8. Size : 83.07 MB
  9. Time : 50:40 min
  10. Url : http://222.222.222/222.222</p><p>1. Whispers In Vain 5:24
  11. 2. Consequence Of Power 4:25
  12. 3. Out Of Nowhere 4:10
  13. 4. Remember 5:30
  14. 5. Mirage 5:05
  15. 6. Episodes Of Mania 5:09
  16. 7. Redemption 5:31
  17. 8. Take Back Yesterday 5:03
  18. 9. Anathema 5:16
  19. 10. Blood Of An Angel 5:07</p><p>Circle II Circles 5th studio album, Consequence Of Power,
  20. is clearly the ultimate masterpiece created by master
  21. vocalist Zak Stevens (ex Savatage) and his band! Strong
  22. hooks, heavy riffs and Zaks unique voice have all fused on
  23. Consequence Of Power to exceed their past catalogue. Watch
  24. out for your favorite album of 2010: Consequence Of Power
  25. by Circle II Circle!</p><p>&nbsp;</p><p>&nbsp;</p><p>http://333.333.333/333.333</p><a href='http://444.444.444/444.444'>http://555.555.555/555.555</a>
复制代码
目标是给字符串里的http://222.222.222/222.222http://333.333.333/333.333加链接
恳请高人给个正则吧
发表于 2010-10-11 12:00:14 | 显示全部楼层
没完全理解~~~
发表于 2010-10-11 14:10:10 | 显示全部楼层
什么意思?
 楼主| 发表于 2010-10-11 14:29:15 | 显示全部楼层
就是给没有加<a>标签的URL加上<a>标签
发表于 2010-10-11 14:38:19 | 显示全部楼层
  1. <?php
  2. $str = '
  3. <p align="center"><img alt="1111" src="http://111.111.111/111.111" /></p><p>Artist : Circle II Circle
  4. Album : Consequence Of Power
  5. Label : AFM
  6. Genre : Metal
  7. Street date : 2010-00-00
  8. Quality : 217 kbps / 44.1kHz / Joint Stereo
  9. Encoder : Lame 3.97 -V2 vbr-new
  10. Size : 83.07 MB
  11. Time : 50:40 min
  12. Url : http://222.222.222/222.222</p><p>1. Whispers In Vain 5:24
  13. 2. Consequence Of Power 4:25
  14. 3. Out Of Nowhere 4:10
  15. 4. Remember 5:30
  16. 5. Mirage 5:05
  17. 6. Episodes Of Mania 5:09
  18. 7. Redemption 5:31
  19. 8. Take Back Yesterday 5:03
  20. 9. Anathema 5:16
  21. 10. Blood Of An Angel 5:07</p><p>Circle II Circles 5th studio album, Consequence Of Power,
  22. is clearly the ultimate masterpiece created by master
  23. vocalist Zak Stevens (ex Savatage) and his band! Strong
  24. hooks, heavy riffs and Zaks unique voice have all fused on
  25. Consequence Of Power to exceed their past catalogue. Watch
  26. out for your favorite album of 2010: Consequence Of Power
  27. by Circle II Circle!</p><p>&nbsp;</p><p>&nbsp;</p><p>http://333.333.333/333.333</p><a href=\'http://444.444.444/444.444\'>http://555.555.555/555.555</a>
  28. ';
  29. function remove_http($matches){
  30.         return str_replace('http://','##########',$matches[0]);
  31. }
  32. $str = preg_replace_callback("/<a[^<>]*href=['"]*http:\/\/[^<>]*>http:\/\/[^<>]*<\/a>/i",'remove_http',$str);
  33. $str = preg_replace_callback("/<img[^<>]*src=['"]*(http:\/\/)[^<>]*>/i",'remove_http',$str);
  34. $str = preg_replace("/http:\/\/[^<>\r\n\s]*/i",'<a href="\0">\0</a>',$str);
  35. $str = str_replace('##########','http://',$str);
  36. echo $str;
  37. ?>
复制代码
求更简单的方法

[ 本帖最后由 gdtv 于 2010-10-11 14:40 编辑 ]
发表于 2010-10-11 15:31:04 | 显示全部楼层
PHP Example: Automatically link URL's inside text.

$text = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $text);
 楼主| 发表于 2010-10-11 15:42:35 | 显示全部楼层

回复 5# 的帖子

谢谢高人

但是如果还能在完美点就好了,现在对于下面这种情况会出问题
<a href='http://555.555.555.555/555.555'>5555555555555</a>
 楼主| 发表于 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标签内情况
 楼主| 发表于 2010-10-11 15:49:10 | 显示全部楼层

回复 8# 的帖子

  1. $str = preg_replace_callback("/<a[^<>]*href=['"]*http:\/\/[^<>]*>[^<>]*<\/a>/i","remove_http",$str);
复制代码
这样好像就OK 了
 楼主| 发表于 2010-10-11 15:53:30 | 显示全部楼层
5楼的方法应该算一个变通的方法
先替换标签内URL的http://
在匹配替换http://的URL
再将第一步被替换的还原

不知道还有没有更好的方法,用(?!exp)这类?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2025-5-3 09:21 , Processed in 0.065942 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表