我刚刚查看了这篇文章(两年后)。可能你已经得到了答案,但是对于初学者来说,你可以使用正则表达式来去除所有类型的 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。看看下面的列表。对于那些想问“是否会剥离 .us、.in、.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
- ?ques==two&t=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