检查一个域是否在 Punycode 中并不容易。需要通过@Wladston 已经说过的规则来实施几次检查。
这是我从ValidateHelper
我的库组合中的类中获取的改编代码示例:PrestaShop CMS 的帮助类。我还添加了测试及其执行结果。
/**
* Validate helper.
*
* @author Maksim T. <zapalm@yandex.com>
*/
class ValidateHelper
{
/**
* Checks if the given domain is in Punycode.
*
* @param string $domain The domain to check.
*
* @return bool Whether the domain is in Punycode.
*
* @see https://developer.mozilla.org/en-US/docs/Mozilla/Internationalized_domain_names_support_in_Mozilla#ASCII-compatible_encoding_.28ACE.29
*
* @author Maksim T. <zapalm@yandex.com>
*/
public static function isPunycodeDomain($domain)
{
$hasPunycode = false;
foreach (explode('.', $domain) as $part) {
if (false === static::isAscii($part)) {
return false;
}
if (static::isPunycode($part)) {
$hasPunycode = true;
}
}
return $hasPunycode;
}
/**
* Checks if the given value is in ASCII character encoding.
*
* @param string $value The value to check.
*
* @return bool Whether the value is in ASCII character encoding.
*
* @see https://en.wikipedia.org/wiki/ASCII
*
* @author Maksim T. <zapalm@yandex.com>
*/
public static function isAscii($value)
{
return ('ASCII' === mb_detect_encoding($value, 'ASCII', true));
}
/**
* Checks if the given value is in Punycode.
*
* @param string $value The value to check.
*
* @return bool Whether the value is in Punycode.
*
* @throws \LogicException If the string is not encoded by UTF-8.
*
* @see https://en.wikipedia.org/wiki/Punycode
*
* @author Maksim T. <zapalm@yandex.com>
*/
public static function isPunycode($value)
{
if (false === static::isAscii($value)) {
return false;
}
if ('UTF-8' !== mb_detect_encoding($value, 'UTF-8', true)) {
throw new \LogicException('The string should be encoded by UTF-8 to do the right check.');
}
return (0 === mb_stripos($value, 'xn--', 0, 'UTF-8'));
}
}
/**
* Test Punycode domain validator.
*
* @author Maksim T. <zapalm@yandex.com>
*/
class Test
{
/**
* Run the test.
*
* @author Maksim T. <zapalm@yandex.com>
*/
public static function run()
{
$domains = [
// White list
'почта@престашоп.рф' => false, // Russian, Unicode
'modulez.ru' => false, // English, ASCII
'xn--80aj2abdcii9c.xn--p1ai' => true, // Russian, ASCII
'xn--80a1acn3a.xn--j1amh' => true, // Ukrainian, ASCII
'xn--srensen-90a.example.com' => true, // German, ASCII
'xn--mxahbxey0c.xn--xxaf0a' => true, // Greek, ASCII
'xn--fsqu00a.xn--4rr70v' => true, // Chinese, ASCII
// Black List
'xn--престашоп.xn--рф' => false, // Russian, Unicode
'xn--prestashop.рф' => false, // Russian, Unicode
];
foreach ($domains as $domain => $isPunycode) {
echo 'TEST: ' . $domain . (ValidateHelper::isPunycodeDomain($domain)
? ' is in Punycode [' . ($isPunycode ? 'OK' : 'FAIL') . ']'
: ' is NOT in Punycode [' . (false === $isPunycode ? 'OK' : 'FAIL') . ']'
) . PHP_EOL;
}
}
}
Test::run();
// The output result:
//
// TEST: почта@престашоп.рф is NOT in Punycode [OK]
// TEST: modulez.ru is NOT in Punycode [OK]
// TEST: xn--80aj2abdcii9c.xn--p1ai is in Punycode [OK]
// TEST: xn--80a1acn3a.xn--j1amh is in Punycode [OK]
// TEST: xn--srensen-90a.example.com is in Punycode [OK]
// TEST: xn--mxahbxey0c.xn--xxaf0a is in Punycode [OK]
// TEST: xn--fsqu00a.xn--4rr70v is in Punycode [OK]
// TEST: xn--престашоп.xn--рф is NOT in Punycode [OK]
// TEST: xn--prestashop.рф is NOT in Punycode [OK]