当前位置: 首页>>代码示例>>PHP>>正文


PHP IP::inet_ptod方法代码示例

本文整理汇总了PHP中IP::inet_ptod方法的典型用法代码示例。如果您正苦于以下问题:PHP IP::inet_ptod方法的具体用法?PHP IP::inet_ptod怎么用?PHP IP::inet_ptod使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IP的用法示例。


在下文中一共展示了IP::inet_ptod方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: range_to_cidrlist

 /**
  * Return a contiguous list of true CIDR blocks that span the range given.
  *
  * Note: It's not a good idea to call this with IPv6 addresses. While it may
  * work for certain ranges this can be very slow. Also an IPv6 list won't be
  * as accurate as an IPv4 list.
  *
  * @example
  *  range_to_cidrlist(192.168.0.0, 192.168.0.15) ==
  *    192.168.0.0/28
  *  range_to_cidrlist(192.168.0.0, 192.168.0.20) ==
  *    192.168.0.0/28
  *    192.168.0.16/30
  *    192.168.0.20/32
  */
 public static function range_to_cidrlist($start, $end)
 {
     $ver = false === filter_var($start, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ? 6 : 4;
     $start = IP::inet_ptod($start);
     $end = IP::inet_ptod($end);
     $len = $ver == 4 ? 32 : 128;
     $log2 = $ver == 4 ? log(2) : BC::bclog(2);
     $list = array();
     while (BC::cmp($end, $start) >= 0) {
         // $end >= $start
         $prefix = self::max_prefix(IP::inet_dtop($start), $len);
         if ($ver == 4) {
             $diff = $len - floor(log($end - $start + 1) / $log2);
         } else {
             // this is not as accurate due to the bclog function
             $diff = bcsub($len, BC::bcfloor(bcdiv(BC::bclog(bcadd(bcsub($end, $start), '1')), $log2)));
         }
         if ($prefix < $diff) {
             $prefix = $diff;
         }
         $list[] = IP::inet_dtop($start) . "/" . $prefix;
         if ($ver == 4) {
             $start += pow(2, $len - $prefix);
         } else {
             $start = bcadd($start, bcpow(2, $len - $prefix));
         }
     }
     return $list;
 }
开发者ID:0151n,项目名称:vichan,代码行数:44,代码来源:CIDR.php

示例2: to_ipv6

 /**
  * Convert an IPv4 address into an IPv6 address.
  *
  * One use-case for this is IP 6to4 tunnels used in networking.
  *
  * @example
  *      to_ipv4("10.10.10.10") == a0a:a0a
  *
  * @param string $ip IPv4 address.
  * @param boolean $mapped If true a Full IPv6 address is returned within the
  *                        official ipv4to6 mapped space "0:0:0:0:0:ffff:x:x"
  */
 public static function to_ipv6($ip, $mapped = false)
 {
     if (!self::isIPv4($ip)) {
         throw new \InvalidArgumentException("Invalid IPv4 address \"{$ip}\"");
     }
     $num = IP::inet_ptod($ip);
     $o1 = dechex($num >> 16);
     $o2 = dechex($num & 0xffff);
     return $mapped ? "0:0:0:0:0:ffff:{$o1}:{$o2}" : "{$o1}:{$o2}";
 }
开发者ID:0151n,项目名称:vichan,代码行数:22,代码来源:IP.php


注:本文中的IP::inet_ptod方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。