示例#1
0
 /**
  * Try get the range instance starting from its string representation.
  *
  * @param string|mixed $range
  *
  * @return static|null
  */
 public static function fromString($range)
 {
     $result = null;
     if (is_string($range) && strpos($range, '*') !== false) {
         if ($range === '*.*.*.*') {
             $result = new static(IPv4::fromString('0.0.0.0'), IPv4::fromString('255.255.255.255'), 4);
         } elseif (strpos($range, '.') !== false && preg_match('/^[^*]+((?:\\.\\*)+)$/', $range, $matches)) {
             $asterisksCount = strlen($matches[1]) >> 1;
             $fromAddress = IPv4::fromString(str_replace('*', '0', $range));
             if ($fromAddress !== null) {
                 $fixedBytes = array_slice($fromAddress->getBytes(), 0, -$asterisksCount);
                 $otherBytes = array_fill(0, $asterisksCount, 255);
                 $toAddress = IPv4::fromBytes(array_merge($fixedBytes, $otherBytes));
                 $result = new static($fromAddress, $toAddress, $asterisksCount);
             }
         } elseif ($range === '*:*:*:*:*:*:*:*') {
             $result = new static(IPv6::fromString('::'), IPv6::fromString('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'), 8);
         } elseif (strpos($range, ':') !== false && preg_match('/^[^*]+((?::\\*)+)$/', $range, $matches)) {
             $asterisksCount = strlen($matches[1]) >> 1;
             $fromAddress = IPv6::fromString(str_replace('*', '0', $range));
             if ($fromAddress !== null) {
                 $fixedWords = array_slice($fromAddress->getWords(), 0, -$asterisksCount);
                 $otherWords = array_fill(0, $asterisksCount, 0xffff);
                 $toAddress = IPv6::fromWords(array_merge($fixedWords, $otherWords));
                 $result = new static($fromAddress, $toAddress, $asterisksCount);
             }
         }
     }
     return $result;
 }
示例#2
0
文件: IPv4.php 项目: mlocati/ip-lib
 /**
  * Create an IPv6 representation of this address.
  *
  * @return IPv6
  */
 public function toIPv6()
 {
     $myBytes = $this->getBytes();
     return IPv6::fromString('2002:' . sprintf('%02x', $myBytes[0]) . sprintf('%02x', $myBytes[1]) . ':' . sprintf('%02x', $myBytes[2]) . sprintf('%02x', $myBytes[3]) . '::');
 }