tôi chỉ kiểm tra bài đăng này (sau 2 năm) có thể là bạn có câu trả lời nhưng đối với những người mới bắt đầu, bạn có thể sử dụng thường xuyên biểu thức để loại bỏ mọi loại URL hoặc Chuỗi truy vấn
(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]+)
nó sẽ loại bỏ mọi loại URL, hãy xem danh sách sau đây. Tôi sử dụng loại khác nhau của lĩnh vực cho những ai muốn hỏi "nó sẽ tước .us, .in hoặc .pk vv loại lĩnh vực hay không.
- 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 == hai & t = p
- http://website.info?ques==two&t=p
- https://www.weborwebsite.com
làm việc Ví dụ (thử nghiệm trong 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);
nó sẽ trở lại
, , , , , , , hello world, working more with ns or and and
Hy vọng nó sẽ giúp rất nhiều trong lập trình ra có
Lưu ý rằng có một phiên bản mới hơn của regex mà ở đây: http://daringfireball.net/2010/ 07/improved_regex_for_matching_urls – IMSoP
Được thực hiện bằng PHP: [http://stackoverflow.com/a/10002262/1055533](http://stackoverflow.com/a/10002262/1055533) – Oskar