Home > php教程 > PHP源码 > body text

简单验证IPv6的PHP代码

WBOY
Release: 2016-06-08 17:27:55
Original
1965 people have browsed it
<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 ?>

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template