l12ab 发表于 2010-10-11 07:23:01

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

求个PHP正则验证URL的帖子

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

对于下面这样的内容不作改动<a href="http://xxx.xxx.com/xxx/xxx.xxx">http://xxx.xxx.com/xxx/xxx.xxx</a><img src="http://xxx.xxx.com/xxx/xxx.xxx">这样的http开头的内容加<a>标签http://xxx.xxx.com/xxx/xxx.xxx给个字符串吧<p align="center"><img alt="1111" src="http://111.111.111/111.111" /></p><p>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</p><p>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</p><p>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!</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.222和http://333.333.333/333.333加链接
恳请高人给个正则吧

zyypp 发表于 2010-10-11 12:00:14

没完全理解~~~

xspoco 发表于 2010-10-11 14:10:10

什么意思?

l12ab 发表于 2010-10-11 14:29:15

就是给没有加<a>标签的URL加上<a>标签

gdtv 发表于 2010-10-11 14:38:19

<?php
$str = '
<p align="center"><img alt="1111" src="http://111.111.111/111.111" /></p><p>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</p><p>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</p><p>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!</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>
';
function remove_http($matches){
        return str_replace('http://','##########',$matches);
}
$str = preg_replace_callback("/<a[^<>]*href=['\"]*http:\/\/[^<>]*>http:\/\/[^<>]*<\/a>/i",'remove_http',$str);
$str = preg_replace_callback("/<img[^<>]*src=['\"]*(http:\/\/)[^<>]*>/i",'remove_http',$str);
$str = preg_replace("/http:\/\/[^<>\r\n\s]*/i",'<a href="\0">\0</a>',$str);
$str = str_replace('##########','http://',$str);
echo $str;
?>求更简单的方法

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

ninjai 发表于 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);

l12ab 发表于 2010-10-11 15:42:35

回复 5# 的帖子

谢谢高人

但是如果还能在完美点就好了,现在对于下面这种情况会出问题
<a href='http://555.555.555.555/555.555'>5555555555555</a>

l12ab 发表于 2010-10-11 15:46:24

原帖由 ninjai 于 2010-10-11 15:31 发表 http://hostloc.wiki/images/common/back.gif
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:10

回复 8# 的帖子

$str = preg_replace_callback("/<a[^<>]*href=['\"]*http:\/\/[^<>]*>[^<>]*<\/a>/i","remove_http",$str);这样好像就OK 了

l12ab 发表于 2010-10-11 15:53:30

5楼的方法应该算一个变通的方法
先替换标签内URL的http://
在匹配替换http://的URL
再将第一步被替换的还原

不知道还有没有更好的方法,用(?!exp)这类?
页: [1] 2
查看完整版本: 坛子里有没有正则高人?求助啊