2010-10-11 72 views
9

我想匹配墙贴中的url链接并用锚标记替换此链接,为此我使用下面的正则表达式。使用正则表达式在php中匹配url模式

我想比赛4种类型的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); 

该表达式仅匹配前两种类型的url。

如果我使用这个表达式匹配url模式 '@(www?([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@',那么它只匹配第三种类型的url模式。

如何将所有四种类型的url模式与单个正则表达式匹配?

回答

14

我会用一个不同的正则表达式来说实话。像这样的一个格鲁伯posted 2009年:

\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))) 

或该更新版本格鲁伯posted在2010年(感谢,@IMSoP):

(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’])) 
+2

注意,这里是正则表达式的新版本在这里:http://daringfireball.net/2010/ 07/improved_regex_for_matching_urls – IMSoP 2012-08-18 17:07:29

+2

在PHP中实现: [http://stackoverflow.com/a/10002262/1055533](http://stackoverflow.com/a/10002262/1055533) – Oskar 2013-08-26 21:05:57

0

如果你想使一个工作,你需要使“https?//”部分可选,因为你似乎对正则表达式有相当好的把握,我不会告诉你,读者的excerise :)

但我普遍同意Nev,它太复杂了为它做什么。

14

使用内华达州斯托克一个完整的工作例子给出的链接:

public function clickableUrls($html){ 
    return $result = preg_replace(
     '%\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))%s', 
     '<a href="$1">$1</a>', 
     $html 
    ); 
} 
+0

我的天啊,终于这个工程...我一直在尝试人们发布的所有类型的语法,或者在语法上有问题或者他们部分工作(我需要修复的是第ere是在网址结尾处被选中的时段,比如t.co/123213 ...) – kn00tcn 2013-01-29 07:11:47

+0

是的,很棒! – Ben 2013-08-11 12:14:48

1

我只是检查这个职位(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类型区或不域。

  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. ?疑问句== 2 &吨= P
  14. http://website.info?ques==two&t=p
  15. 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 

希望它可以帮助很多程序员在那里

2

我环顾四周,没有看到任何我需要的是什么。我发现this one这是接近,所以我修改,如下所示:

^((([hH][tT][tT][pP][sS]?)\:\/\/)?([\w\\-]+(\[\w\.\&%\$\-]+)*)?((([^\s\(\)\<\>\\\"\.\ [\]\,;:]+)(\.[^\s\(\)\<\>\\\"\.\[\]\,;:]+)*(\.[a-zA-Z]{2,4}))|((([01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}([01]?\d{1,2}|2[0-4]\d|25[0-5])))(\b\:(6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3}|0)\b)?((\/[^\/][\w\.\,\?\'\\\/\+&%\$#\=~_\-]*)*[^\.\,\?\"\'\(\)\[\]!;<>{}\s\x7F-\xFF])?)$ 

检查出来的debuggex

0

使用此模式。

$regex = "(https?\:\/\/|ftp\:\/\/|www\.|[a-z0-9-]+)+([a-z0-9-]+)\.+([a-z]{2,4})((\/|\.)+([a-z0-9-_.\/]*)$|$)"; 

希望有帮助。

0

我的两分钱(五年后!):

preg_match("/^((https|http|ftp)\:\/\/)?([a-z0-9A-Z]+\.[a-z0-9A-Z]+\.[a-z0-9A-Z]+\.[a-zA-Z]{2,4}|[a-z0-9A-Z]+\.[a-z0-9A-Z]+\.[a-zA-Z]{2,4}|[a-z0-9A-Z]+\.[a-zA-Z]{2,4})$/i", $url) 

希望它可以帮助别人