match url pattern in php using regular expression

2019-01-18 12:58发布

I want to match a url link in wall post and replace this link with anchor tag, for this I use the regular expression below.

I would like the match 4 types of url:

  1. http://example.com
  2. https://example.com
  3. www.example.com
  4. example.com
preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@',
             '<a href="$1">$1</a>', $subject);

This expression matches only first two types of url.

If I use this expression for match url pattern '@(www?([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', then it only matches the third type of url pattern.

How can I match all four type of url pattern with a single regular expression?

标签: php regex url
7条回答
Anthone
2楼-- · 2019-01-18 13:24

I just checked this post (after 2 years) might be you got the answer but for those who are beginners, you can use regular expression to strip every type of URL or Query String

(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]+)

it will strip every type of URLs, take a look at the following list. I used different type of domains for those who want to ask "will it strip .us, .in or .pk etc type of domains or not.

  1. ftp://www.web.com
  2. web.net
  3. www.website.info
  4. website.us
  5. web.ws?query=true
  6. www.web.biz?query=true
  7. ftp://web.in?query=true
  8. media.google.com
  9. ns.google.pk
  10. ww1.smart.au
  11. www3.smart.br
  12. w1.smart.so
  13. ?ques==two&t=p
  14. http://website.info?ques==two&t=p
  15. https://www.weborwebsite.com

Working Example (tested in 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);

it will return

, , , , , , , hello world, working more with ns or and and

Hope it helps a lot of coders out there

查看更多
登录 后发表回答