Returns true if and only if the $value is a valid hostname with respect to the current allow option
Match against IDN hostnames Note: Keep label regex short to avoid issues with long patterns when matching IDN hostnames
1688 if (!is_string(
$value)) {
1689 $this->
_error(self::INVALID);
1695 if (preg_match(
'/^[0-9a-f:.]*$/i',
$value) &&
1697 if (!($this->_options[
'allow'] & self::ALLOW_IP)) {
1698 $this->
_error(self::IP_ADDRESS_NOT_ALLOWED);
1715 if ($this->_options[
'allow'] & self::ALLOW_LOCAL) {
1716 if (substr(
$value, -1) ===
'.') {
1718 if (substr(
$value, -1) ===
'.') {
1720 $this->
_error(self::INVALID_LOCAL_NAME);
1726 $domainParts = explode(
'.',
$value);
1729 if ((count($domainParts) == 4) && preg_match(
'/^[0-9.a-e:.]*$/i',
$value) &&
1731 $this->
_error(self::INVALID_LOCAL_NAME);
1735 if ((count($domainParts) > 1) && (strlen(
$value) >= 4) && (strlen(
$value) <= 254)) {
1738 $origenc = PHP_VERSION_ID < 50600
1739 ? iconv_get_encoding(
'internal_encoding')
1741 if (PHP_VERSION_ID < 50600) {
1742 iconv_set_encoding(
'internal_encoding',
'UTF-8');
1744 ini_set(
'default_charset',
'UTF-8');
1749 if (preg_match(
'/([^.]{2,63})$/iu', end($domainParts), $matches)
1750 || (array_key_exists(end($domainParts), $this->_validIdns))) {
1751 reset($domainParts);
1759 $this->_tld = $matches[1];
1760 if ($this->_options[
'tld']) {
1761 if (!in_array(strtolower($this->_tld), $this->_validTlds)
1762 && !in_array($this->_tld, $this->_validTlds)) {
1763 $this->
_error(self::UNKNOWN_TLD);
1769 array_pop($domainParts);
1777 $regexChars = array(0 =>
'/^[a-z0-9\x2d]{1,63}$/i');
1778 if ($this->_options[
'idn'] && isset($this->_validIdns[strtoupper($this->_tld)])) {
1779 if (is_string($this->_validIdns[strtoupper($this->_tld)])) {
1780 $regexChars += include($this->_validIdns[strtoupper($this->_tld)]);
1782 $regexChars += $this->_validIdns[strtoupper($this->_tld)];
1788 foreach ($domainParts as $domainPart) {
1790 if (empty($domainPart) && $domainPart !==
'0') {
1791 $this->
_error(self::INVALID_HOSTNAME);
1796 if (strpos($domainPart,
'xn--') === 0) {
1798 if ($domainPart ===
false) {
1804 if ((strpos($domainPart,
'-') === 0)
1805 || ((strlen($domainPart) > 2) && (strpos($domainPart,
'-', 2) == 2) && (strpos($domainPart,
'-', 3) == 3))
1806 || (strpos($domainPart,
'-') === (strlen($domainPart) - 1))) {
1807 $this->
_error(self::INVALID_DASH);
1814 foreach($regexChars as $regexKey => $regexChar) {
1815 $status = preg_match($regexChar, $domainPart);
1818 if (array_key_exists(strtoupper($this->_tld), $this->_idnLength)
1819 && (array_key_exists($regexKey, $this->_idnLength[strtoupper($this->_tld)]))) {
1820 $length = $this->_idnLength[strtoupper($this->_tld)];
1823 if (iconv_strlen($domainPart,
'UTF-8') > $length) {
1824 $this->
_error(self::INVALID_HOSTNAME);
1838 if ($check !== count($domainParts)) {
1839 $this->
_error(self::INVALID_HOSTNAME_SCHEMA);
1844 $this->
_error(self::UNDECIPHERABLE_TLD);
1849 if (PHP_VERSION_ID < 50600) {
1850 iconv_set_encoding(
'internal_encoding', $origenc);
1852 ini_set(
'default_charset', $origenc);
1856 if (
$status && ($this->_options[
'allow'] & self::ALLOW_DNS)) {
1859 }
else if ($this->_options[
'allow'] & self::ALLOW_DNS) {
1860 $this->
_error(self::INVALID_HOSTNAME);
1864 if ($this->_options[
'allow'] & self::ALLOW_URI) {
1865 if (preg_match(
"/^([a-zA-Z0-9-._~!$&\'()*+,;=]|%[[:xdigit:]]{2}){1,254}$/i",
$value)) {
1868 $this->
_error(self::INVALID_URI);
1873 $regexLocal =
'/^(([a-zA-Z0-9\x2d]{1,63}\x2e)*[a-zA-Z0-9\x2d]{1,63}[\x2e]{0,1}){1,254}$/';
1885 $this->
_error(self::INVALID_LOCAL_NAME);
1889 if (
$status && !$allowLocal) {
1890 $this->
_error(self::LOCAL_NAME_NOT_ALLOWED);
ini_set($varName, $newValue)
_error($messageKey, $value=null)
setTranslator($translator=null)