SimplePie_Net_IPv6::checkIPv6 PHP 메소드

checkIPv6() 공개 메소드

Checks if the given IP is IPv6-compatible
public checkIPv6 ( string $ip ) : boolean
$ip string a valid IPv6-address
리턴 boolean true if $ip is an IPv6 address
    function checkIPv6($ip)
    {
        $ipPart = SimplePie_Net_IPv6::SplitV64($ip);
        $count = 0;
        if (!empty($ipPart[0])) {
            $ipv6 = explode(':', $ipPart[0]);
            for ($i = 0; $i < count($ipv6); $i++) {
                $dec = hexdec($ipv6[$i]);
                $hex = strtoupper(preg_replace('/^[0]{1,3}(.*[0-9a-fA-F])$/', '\\1', $ipv6[$i]));
                if ($ipv6[$i] >= 0 && $dec <= 65535 && $hex === strtoupper(dechex($dec))) {
                    $count++;
                }
            }
            if ($count === 8) {
                return true;
            } elseif ($count === 6 && !empty($ipPart[1])) {
                $ipv4 = explode('.', $ipPart[1]);
                $count = 0;
                foreach ($ipv4 as $ipv4_part) {
                    if ($ipv4_part >= 0 && $ipv4_part <= 255 && preg_match('/^\\d{1,3}$/', $ipv4_part)) {
                        $count++;
                    }
                }
                if ($count === 4) {
                    return true;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

Usage Example

예제 #1
0
 public function set_host($host)
 {
     if ($host === null || $host === '') {
         $this->host = null;
         $this->valid[__FUNCTION__] = true;
         return true;
     } elseif ($host[0] === '[' && substr($host, -1) === ']') {
         if (SimplePie_Net_IPv6::checkIPv6(substr($host, 1, -1))) {
             $this->host = $host;
             $this->valid[__FUNCTION__] = true;
             return true;
         } else {
             $this->host = null;
             $this->valid[__FUNCTION__] = false;
             return false;
         }
     } else {
         $this->host = $this->replace_invalid_with_pct_encoding($host, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~!$&\'()*+,;=', SIMPLEPIE_LOWERCASE);
         $this->valid[__FUNCTION__] = true;
         return true;
     }
 }