我只是检查这个职位(2年后)可能是你得到了答案,但对于那些谁是初学者,你可以使用普通的表达式剥离每种类型的URL或查询字符串
(https|http|ftp)\:\/\/|([a-z0-9A-Z]+\.[a-z0-9A-Z]+\.[a-zA-Z]{2,4})|([a-z0-9A-Z]+\.[a-zA-Z]{2,4})|\?([a-zA-Z0-9]+[\&\=\#a-z]+)
它将剥离每种类型的URL,请看下面的列表。我用不同的类型,谁想要问“,将它剥离页面没有自动跳转,或者。在等的.pk类型区或不域。
- ftp://www.web.com
- web.net
- www.website .INFO
- website.us
- web.ws?query=true
- www.web.biz?query=true
- ftp://web.in?query=true
- media.google。COM
- ns.google.pk
- ww1.smart.au
- www3.smart.br
- w1.smart.so
- ?疑问句== 2 &吨= P
- http://website.info?ques==two&t=p
- https://www.weborwebsite.com
工作实施例(在PHP5 +,+的Apache2测试):
$str = "ftp://www.web.com, web.net, www.website.info, website.us, web.ws?query=true, www.web.biz?query=true, ftp://web.in?query=true, media.google.com hello world, working more with ns ns.google.pk or ww1.smart.au and www3.smart.br w1.smart.so ?ques==two&t=p http://website.info?ques==two&t=p https://www.weborwebsite.com and ftp://www.hotmail.br";
echo preg_replace("/(https|http|ftp)\:\/\/|([a-z0-9A-Z]+\.[a-z0-9A-Z]+\.[a-zA-Z]{2,4})|([a-z0-9A-Z]+\.[a-zA-Z]{2,4})|\?([a-zA-Z0-9]+[\&\=\#a-z]+)/i", "", $str);
它将返回
, , , , , , , hello world, working more with ns or and and
希望它可以帮助很多程序员在那里
注意,这里是正则表达式的新版本在这里:http://daringfireball.net/2010/ 07/improved_regex_for_matching_urls – IMSoP 2012-08-18 17:07:29
在PHP中实现: [http://stackoverflow.com/a/10002262/1055533](http://stackoverflow.com/a/10002262/1055533) – Oskar 2013-08-26 21:05:57