php 判断是否ipv6,简单验证IPv6的PHP代码

$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 ?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值