首頁  >  文章  >  php教程  >  简单验证IPv6的PHP代码

简单验证IPv6的PHP代码

WBOY
WBOY原創
2016-06-08 17:27:551966瀏覽
<script>ec(2);</script>


$ip="2001:0db8:85a3:08d3:1319:8a2e:0370:7334"; 

02 //$ip="2001:0db8:85a3:08d3::7334"; 

03 if(filter_var($ip,FILTER_VALIDATE_IP,FILTER_FLAG_IPV6)!=false) 

04   

05 { 

06 echo $ip." is an IPv6 Address"; 

07 }else

08 { 

09 echo $ip." is NOT an IPv6 Address"; 

10 } 

11 //PHP 5.2.0.x版本通过测试


filter_var() 函数通过指定的过滤器过滤变量。
如果成功,则返回已过滤的数据,如果失败,则返回 false

 

02 // literally from the ABNF in rfc3986 (thanks to 'WCP')  

03 function validateIPv6($IP)  

04 {  

05     return preg_match('/A  

06         (?:  

07             (?:  

08                     (?:[a-f0-9]{1,4}:){6}  

09                 |  

10                     ::(?:[a-f0-9]{1,4}:){5}  

11                 |  

12                     (?:[a-f0-9]{1,4})?::(?:[a-f0-9]{1,4}:){4}  

13                 |  

14                     (?:(?:[a-f0-9]{1,4}:){0,1}[a-f0-9]{1,4})?::(?:[a-f0-9]{1,4}:){3}  

15                 |  

16                     (?:(?:[a-f0-9]{1,4}:){0,2}[a-f0-9]{1,4})?::(?:[a-f0-9]{1,4}:){2}  

17                 |  

18                     (?:(?:[a-f0-9]{1,4}:){0,3}[a-f0-9]{1,4})?::[a-f0-9]{1,4}:  

19                 |  

20                     (?:(?:[a-f0-9]{1,4}:){0,4}[a-f0-9]{1,4})?::  

21             )  

22                 (?:  

23                         [a-f0-9]{1,4}:[a-f0-9]{1,4}  

24                     |  

25                         (?:(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).){3}  

26                             (?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])  

27                 )  

28             |  

29                 (?:  

30                         (?:(?:[a-f0-9]{1,4}:){0,5}[a-f0-9]{1,4})?::[a-f0-9]{1,4}  

31                     |  

32                         (?:(?:[a-f0-9]{1,4}:){0,6}[a-f0-9]{1,4})?::  

33                 )  

34         )Z/ix',  

35         $IP 

36     );  

37 }  

38 ?>

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn