SimplePie_Net_IPv6::check_ipv6() – Checks an IPv6 address

You appear to be a bot. Output may be restricted

Description

Checks an IPv6 address

Checks if the given IP is a valid IPv6 address

Usage

$bool = SimplePie_Net_IPv6::check_ipv6( $ip );

Parameters

$ip
( string ) required – An IPv6 address

Returns

bool true if $ip is a valid IPv6 address

Source

File name: wordpress/wp-includes/SimplePie/Net/IPv6.php
Lines:

1 to 42 of 42
  public static function check_ipv6($ip)
  {
    $ip = self::uncompress($ip);
    list($ipv6, $ipv4) = self::split_v6_v4($ip);
    $ipv6 = explode(':', $ipv6);
    $ipv4 = explode('.', $ipv4);
    if (count($ipv6) === 8 && count($ipv4) === 1 || count($ipv6) === 6 && count($ipv4) === 4)
    {
      foreach ($ipv6 as $ipv6_part)
      {
        // The section can't be empty
        if ($ipv6_part === '')
          return false;

        // Nor can it be over four characters
        if (strlen($ipv6_part) > 4)
          return false;

        // Remove leading zeros (this is safe because of the above)
        $ipv6_part = ltrim($ipv6_part, '0');
        if ($ipv6_part === '')
          $ipv6_part = '0';

        // Check the value is valid
        $value = hexdec($ipv6_part);
        if (dechex($value) !== strtolower($ipv6_part) || $value < 0 || $value > 0xFFFF)
          return false;
      }
      if (count($ipv4) === 4)
      {
        foreach ($ipv4 as $ipv4_part)
        {
          $value = (int) $ipv4_part;
          if ((string) $value !== $ipv4_part || $value < 0 || $value > 0xFF)
            return false;
        }
      }
      return true;
    }

    return false;
  }
 

 View on GitHub View on Trac