好的,我使用PHP类(http://idnaconv.net/index.html)来编码/解码域名。
不幸的是,它似乎没有提供一个接口来检查域名是否已经是punycode。
实现这一目标的最好方法是什么?这将是很好的,如果有人可以张贴源代码如何验证域是punycode或没有(有解释,因为idna_convert代码不是真的对我来说)。我已经知道如何从idna_convert捕获异常。:-)
顺便说一句:当您尝试将域名转换为已经是punycode的punycode时,idna_convert会抛出异常(参见https://github.com/phlylabs/idna-convert/blob/master/src/Punycode.php;第157行)。此外,我真的不明白他们的支票是如何工作的。
发布于 2016-05-13 20:05:03
最简单的方法就是转换它,并检查结果是否等于输入。
EDIT:你可以通过如下的检查来扩展Punycode类:
class PunycodeCheck extends Punycode
{
public function check_encoded($decoded)
{
$extract = self::byteLength(self::punycodePrefix);
$check_pref = $this->UnicodeTranscoder->utf8_ucs4array(self::punycodePrefix);
$check_deco = array_slice($decoded, 0, $extract);
if ($check_pref == $check_deco)
return true;
return false;
}
}
发布于 2016-05-20 06:53:40
这取决于你到底想要什么。
作为第一个基本检查,查看域名是否只包含ASCII字符。如果是,则该域“已经是punycode",从某种意义上说,它不能进一步转换。有关检查字符串是否只包含ASCII字符的信息,请参见Determine if UTF-8 text is all ASCII?。
如果最重要的是,您想要检查域是否为IDN形式,在点.
处分开域,并检查是否有任何以xn--
开头的子串。
如果除此之外,您还想检查域名是否为IDN以及是否有效,只需尝试使用库的decode函数进行解码即可。
发布于 2018-12-27 11:07:42
检查一个域是否在Punycode中并不是一件很容易的事情。根据@Wladston已经说过的规则,需要进行几次检查才能实现。
这是我从我的库:Helper classes for PrestaShop CMS的组合中提取的ValidateHelper
类的改编代码示例。我还添加了测试及其执行结果。
/**
* 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]
https://stackoverflow.com/questions/37209239
复制相似问题