VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • php匹配网址的正则表达式

URI 是 Web上可用的每种资源 - HTML文档、图像、视频片段、程序等 - 由一个通用资源标志符(Uniform Resource Identifier, 简称"URI")进行定位.

对就分组:

  1. ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(?([^#]*))?(#(.*))? 
  2.  12            3  4          5       6  7        8 9 

例,代码如下:

正则表达式 (http|ftp|https)://[w-_]+(.[w-_]+)+([w-.,@?^=%&:/~+#]*[w-@?^=%&/~+#])? 

匹配 http://regxlib.com/Default.aspx | http://electronics.cnet.com/electronics/0-6342366-8-8994967-1.html 

不匹配 www.phpfensi.com 

正则表达式 ^\{2}[w-]+\(([w-][w-s]*[w-]+[$$]?$)|([w-][$$]?$)) 

匹配 \serverservice | \servermy service | \serv_001service$ 
不匹配 \my serverservice | \server service | \server$service

正则表达式 ^(http|https|ftp)://([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)?((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9-]+.)*[a-zA-Z0-9-]+.[a-zA-Z]{2,4})(:[0-9]+)?(/[^/][a-zA-Z0-9.,?'\/+&%$#=~_-@]*)*$ 

匹配 http://www.sysrage.net | https://64.81.85.161/site/file.php?cow=moo's |ftp://user:pass@host.com:123 

不匹配 phpfensi.com

正则表达式 ^([a-zA-Z]:|\\[^/\:*?"<>|]+\[^/\:*?"<>|]+)(\[^/\:*?"<>|]+)+(.[^/\:*?"<>|]+)$ 

匹配 c:Test.txt | \serversharedTest.txt | \serversharedTest.t 

不匹配 c:Test | \servershared | \serversharedTest.?

正则表达式 ^(http|https|ftp)://([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&amp;%$-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9-]+.)*[a-zA-Z0-9-]+.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(/($|[a-zA-Z0-9.,?'\+&amp;%$#=~_-]+))*$ 

匹配 http://site.com/dir/file.php?var=moo | https://localhost |ftp://user:pass@site.com:21/file/dir 

不匹配 site.com | http://site.com/dir//

正则表达式 ^([a-zA-Z]:)(\[^\/:*?<>"|]*(?<![ ]))*(.[a-zA-Z]{2,6})$ 

匹配 C:di___rfi_sysle.txt | c:dirfilename.txt 

不匹配 c:dirfile?name.txt

正则表达式 ^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?.)+[a-zA-Z]{2,6}$ 

匹配 regexlib.com | this.is.a.museum | 3com.com 

不匹配 notadomain-.com | helloworld.c | .oops.org

正则表达式 ^(((ht|f)tp(s?))://)?(www.|[a-zA-Z].)[a-zA-Z0-9-.]+.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(:[0-9]+)*(/($|[a-zA-Z0-9.,;?'\+&amp;%$#=~_-]+))*$ 

匹配 www.blah.com:8103 | www.blah.com/blah.asp?sort=ASC |www.blah.com/blah.htm#blah 

不匹配 www.state.ga | http://www.blah.ru

正则表达式 b(([w-]+://?|www[.])[^s()<>]+(?:([wd]+)|([^[:punct:]s]|/))) 

匹配 http://foo.com/blah_blah | http://foo.com/blah_blah/ | (Something like http://foo.com/blah_blah) | http://foo.com/blah_blah_(wikipedia) | (Something like http://foo.com/blah_blah_(wikipedia)) | http://foo.com/blah_blah. |http://foo.com/blah_blah/. | <http://foo.com/blah_blah> | <http://foo.com/blah_blah/>| http://foo.com/blah_blah, | http://www.example.com/wpstyle/?p=364. | http://?df.ws/123 | rdar://1234 | rdar:/1234 | http://userid:password@example.com:8080 |http://userid@example.com | http://userid@example.com:8080 |http://userid:password@example.com 

不匹配 no_ws.example.com | no_proto_or_ws.com | /relative_resource.php

可以匹配任何网址,代码如下:

  1. <script type="text/javascript">  
  2. function IsURL(urlString)  
  3. {  
  4. regExp = /^((https?|ftp|news)://)?([a-z]([a-z0-9-]*[.。])+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)|(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))(/[a-z0-9_-.~]+)*(/([a-z0-9_-.]*)(?[a-z0-9+_-.%=&]*)?)?(#[a-z][a-z0-9_]*)?$/  
  5. if (urlString.match(regExp))  
  6. return "is a url";  
  7. else  
  8. return "not a url";  
  9. }  
  10. url = "";  
  11. alert(IsURL(url));  
  12. </script> 

PHP 测试,代码如下:

  1. <?php 
  2. $search = '~^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(?([^#]*))?(#(.*))?~i'
  3. $url = 'http://www.php.net/pub/ietf/uri/#Related'
  4. $url = trim($url); 
  5. preg_match_all($search$url ,$rr); 
  6. printf("<p>输出URL数据为:</p><pre>%s</pre>n",var_export( $rr ,TRUE)); 
  7.  
  8. /* 
  9. 各分组如下 
  10.       $1 = http: 
  11.       $2 = http 
  12.       $3 = //www.php.net 
  13.       $4 = www.php.net 
  14.       $5 = /pub/ietf/uri/ 
  15.       $6 = <undefined> 
  16.       $7 = <undefined> 
  17.       $8 = #Related 
  18.       $9 = Related 
  19. */ 
  20. ?> 

百度上看到另外一块简洁的代码:

  1. <?php  
  2. // 从 URL 中取得主机名  
  3. preg_match("/^(http://)?([^/]+)/i""http://www.php.net/index.html"$matches);  
  4. $host = $matches[2];  
  5. // 从主机名中取得后面两段  
  6. preg_match("/[^./]+.[^./]+$/"$host$matches);  
  7. echo "domain name is: {$matches[0]}n";  
  8. ?>  
  9. //执行后输出:domain name is: php.net 
  10.  



出处:http://www.phpfensi.com/php/20140725/3961.html


相关教程