如何删除除bbcode之外的网址

时间:2014-07-07 08:29:36

标签: php regex url preg-replace pcre

好吧我有一些php问题... 基本上我试图阻止我的网站链接....以防止垃圾邮件 我得到了一些,但我还是想让玩家发布图片...... 你能帮忙吗?

以下是即时输入

Img
[img]http://www.blank........gif[/img]
link
http://www.blank...........=compose&ID=1
...
test
test
.com
.biz
.net
.org
test
...
Img
[img]http://www.blank..........gif[/img]

以下是我想改变的内容(php明智)

Img
 Show Image(since its inside [img] brackets)
link
[URL removed]
...
test
test
[URL removed]
[URL removed]
[URL removed]
[URL removed]
test
...
Img
 Show Image(since its inside [img] brackets)

$msg = trim(preg_replace('#([^\s]*)(http://|\.com|\.net|\.org|\.biz|\.info|\.pt|\.php|ref\=| www| DOT )([^\s]*)#i' , ' [URL removed] ', $msg));
*This works perfect.... except... dont allow images*

$msg = preg_replace( array('#(?<!\[img\])(http://)#i' , '#(\.com|\.net|\.org|\.biz|\.info|\.pt| DOT | www|ref\=|\.php)#i' ) , '[URL removed]', $msg);
*this does this*


Img
(broken Image)
link
[URL removed]www.blank[URL removed]/mailbox[URL removed]?action=compose&ID=1
...
test
test
awww
www
[URL removed]
[URL removed]
[URL removed]
[URL removed]
[URL removed]
test
...
Img
(broken Image)

1 个答案:

答案 0 :(得分:0)

我不是正则表达大师,但我认为应该做的工作:

<?php

$pattern = "Img
[img]     http://www.blank........gif[/img]
link
http://www.blank...........=compose&ID=1
...
test
test
.com
.biz
.net
.org
test
...
Img
[img]http://www.blank..........gif[/img]";

$pattern = preg_replace('#\[img\](\s)*#i','[img]', $pattern);

$pattern = preg_replace('#(?<!\[img\])((http(s|):\/\/|ftp(s|):\/\/)(\S*)\.(\S*)|(\S*\.(com|net|org|biz|info)))(?!\[\\img\])(?:(?=(\s|\Z)))#i','[URL removed]',$pattern);


echo nl2br($pattern);

第一个preg_replace用于删除导致第二次替换问题的[img]之后的空格。如果您对第一个preg_replace发表评论,您会看到$pattern我插入的[img]内的链接也将被替换。

当然,我只将样本域(com|net|org|biz|info)置于模式中,您应该填写更多:ru,uk等等。