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


PHP Net_IPv6::getPrefixLength方法代码示例

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


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

示例1: compress

 /**
  * Compresses an IPv6 adress
  *
  * RFC 2373 allows you to compress zeros in an adress to '::'. This
  * function expects an valid IPv6 adress and compresses successive zeros
  * to '::'
  *
  * Example:  FF01:0:0:0:0:0:0:101   -> FF01::101
  *           0:0:0:0:0:0:0:1        -> ::1
  *
  * Whe $ip is an already compressed adress the methode returns the value as is,
  * also if the adress can be compressed further.
  *
  * Example: FF01::0:1 -> FF01::0:1
  *
  * To enforce maximum compression, you can set the second argument $force to true.
  *
  * Example: FF01::0:1 -> FF01::1
  *
  * @param String  $ip    a valid IPv6-adress (hex format)
  * @param boolean $force if true the adress will be compresses as best as possible (since 1.2.0)
  *
  * @return tring the compressed IPv6-adress (hex format)
  * @access public
  * @see    Uncompress()
  * @static
  * @author elfrink at introweb dot nl
  */
 public static function compress($ip, $force = false)
 {
     if (false !== strpos($ip, '::')) {
         // its already compressed
         if (true == $force) {
             $ip = Net_IPv6::uncompress($ip);
         } else {
             return $ip;
         }
     }
     $prefix = Net_IPv6::getPrefixLength($ip);
     if (false === $prefix) {
         $prefix = '';
     } else {
         $ip = Net_IPv6::removePrefixLength($ip);
         $prefix = '/' . $prefix;
     }
     $netmask = Net_IPv6::getNetmaskSpec($ip);
     $ip = Net_IPv6::removeNetmaskSpec($ip);
     $ipp = explode(':', $ip);
     for ($i = 0; $i < count($ipp); $i++) {
         $ipp[$i] = dechex(hexdec($ipp[$i]));
     }
     $cip = ':' . join(':', $ipp) . ':';
     preg_match_all("/(:0)(:0)+/", $cip, $zeros);
     if (count($zeros[0]) > 0) {
         $match = '';
         foreach ($zeros[0] as $zero) {
             if (strlen($zero) > strlen($match)) {
                 $match = $zero;
             }
         }
         $cip = preg_replace('/' . $match . '/', ':', $cip, 1);
     }
     $cip = preg_replace('/((^:)|(:$))/', '', $cip);
     $cip = preg_replace('/((^:)|(:$))/', '::', $cip);
     if ('' != $netmask) {
         $cip = $cip . '/' . $netmask;
     }
     return $cip . $prefix;
 }
开发者ID:martinsv,项目名称:phpipam,代码行数:69,代码来源:IPv6.php


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