坛子里有没有正则高人?求助啊
求个PHP正则验证URL的帖子我的网站文章中有些链接作了xxx,有的没有,我想用正则将全站没有加、标签的http开头的链接都加上链接应该怎么写正则呢?
对于下面这样的内容不作改动[*]http://xxx.xxx.com/xxx/xxx.xxx复制代码[*]
复制代码这样的http开头的内容加标签[*]http://xxx.xxx.com/xxx/xxx.xxx复制代码给个字符串吧[*]
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复制代码目标是给字符串里的http://222.222.222/222.222和http://333.333.333/333.333加链接
恳请高人给个正则吧 没完全理解~~~ 什么意思? 就是给没有加标签的URL加上标签 [*]
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
[*]';
[*]function remove_http($matches){
[*] return str_replace('http://','##########',$matches);
[*]}
[*]$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",'\0',$str);
[*]$str = str_replace('##########','http://',$str);
[*]echo $str;
[*]?>复制代码求更简单的方法
[ 本帖最后由 gdtv 于 2010-10-11 14:40 编辑 ] PHP Example: Automatically link URL's inside text.
$text = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '$1', $text); 谢谢高人
但是如果还能在完美点就好了,现在对于下面这种情况会出问题
5555555555555 原帖由 ninjai 于 2010-10-11 15:31 发表
back.gif
PHP Example: Automatically link URL's inside text.
$text = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '$1', $text);
这个没有考虑已经在img和a标签内情况 [*]$str = preg_replace_callback("/]*href=['"]*http:\/\/[^]*>[^]*/i","remove_http",$str);复制代码这样好像就OK 了 5楼的方法应该算一个变通的方法
先替换标签内URL的http://
在匹配替换http://的URL
再将第一步被替换的还原
不知道还有没有更好的方法,用(?!exp)这类?求正则帝,求一行正则代码实现的方法
页:
[1]
2