全球主机交流论坛

标题: 坛子里有没有正则高人?求助啊 [打印本页]

作者: l12ab    时间: 2010-10-11 07:23
标题: 坛子里有没有正则高人?求助啊
求个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加链接
恳请高人给个正则吧
作者: zyypp    时间: 2010-10-11 12:00
没完全理解~~~
作者: xspoco    时间: 2010-10-11 14:10
什么意思?
作者: l12ab    时间: 2010-10-11 14:29
就是给没有加<a>标签的URL加上<a>标签
作者: gdtv    时间: 2010-10-11 14:38
  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 编辑 ]
作者: ninjai    时间: 2010-10-11 15:31
PHP Example: Automatically link URL's inside text.

$text = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $text);
作者: l12ab    时间: 2010-10-11 15:42
标题: 回复 5# 的帖子
谢谢高人

但是如果还能在完美点就好了,现在对于下面这种情况会出问题
<a href='http://555.555.555.555/555.555'>5555555555555</a>
作者: l12ab    时间: 2010-10-11 15:46
原帖由 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标签内情况
作者: l12ab    时间: 2010-10-11 15:49
标题: 回复 8# 的帖子
  1. $str = preg_replace_callback("/<a[^<>]*href=['"]*http:\/\/[^<>]*>[^<>]*<\/a>/i","remove_http",$str);
复制代码
这样好像就OK 了
作者: l12ab    时间: 2010-10-11 15:53
5楼的方法应该算一个变通的方法
先替换标签内URL的http://
在匹配替换http://的URL
再将第一步被替换的还原

不知道还有没有更好的方法,用(?!exp)这类?
作者: gdtv    时间: 2010-10-11 16:25
求正则帝,求一行正则代码实现的方法
作者: l12ab    时间: 2010-10-11 16:41
标题: 回复 11# 的帖子
hehe 铜球
作者: l12ab    时间: 2010-10-11 17:33
有高人给出了方案
  1. $str = preg_replace('%((?>http://[^\s<>\'"]+))(?![^<>]+/?>|</a>)%i', '<a href="\1" target="_blank">\1</a>', $str);
复制代码
http://regex.me/thread-1276-1-1.html




欢迎光临 全球主机交流论坛 (https://loc.193.gs/) Powered by Discuz! X3.4