示例#1
0
 /**
  * Checks whether the given host name is valid according to the syntax defined
  * in RFC-3986.
  *
  * Note that a valid host name is not restricted to be a valid IPv4, IPv6 or
  * domain name. The valid syntax is much more abstract.
  *
  * Returns TRUE in case the given host name is valid.
  * Otherwise returns FALSE.
  *
  * @param string $host The host name to check
  * @return bool
  */
 public static function isValidHost($host)
 {
     // check whether a string is given
     if (!is_string($host)) {
         return false;
     }
     // remove surrounding square brackets from host name
     $host = StringUtil::removeEnclosingCharacters($host, '[', ']');
     // check for registered name (also covers IPv4 addresses)
     if (preg_match('/^(?:[' . self::CHARS_UNRESERVED . self::CHARS_SUB_DELIMITERS . ']+|%[[:xdigit:]]{2})+$/', $host)) {
         return true;
     }
     // check for IPv6
     if (filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false) {
         return true;
     }
     // check for IPvFuture
     if (preg_match('/^v[[:xdigit:]]+\\.[' . self::CHARS_UNRESERVED . self::CHARS_SUB_DELIMITERS . ':]+$/', $host)) {
         return true;
     }
     // not a valid host
     return false;
 }