PHP 8.3.4 Released!

idn_to_utf8

(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL intl >= 1.0.2, PECL idn >= 0.1)

idn_to_utf8将域名从 IDNA ASCII 转换为 Unicode

说明

过程化风格

idn_to_utf8(
    string $domain,
    int $flags = IDNA_DEFAULT,
    int $variant = INTL_IDNA_VARIANT_UTS46,
    array &$idna_info = null
): string|false

该函数将 Unicode 域名从 IDNA ASCII 兼容格式转换为 纯 Unicode(UTF-8 编码)。

参数

domain

要转换的 IDNA ASCII 兼容格式的域名。

flags

转换选项 — IDNA_* 开头的常量(除 IDNA_ERROR_* 开头的常量)。

variant

对于 IDNA 2003 是 INTL_IDNA_VARIANT_2003 (自 PHP 7.2.0 起已弃用), 对于 UTS #46 是 INTL_IDNA_VARIANT_UTS46 (仅 ICU 4.6 起可用)。

idna_info

仅当 INTL_IDNA_VARIANT_UTS46 用于 variant 时,才可以使用该参数。 在这种情况下,它将用这些键组成的数组来填充: 'result' 键,转换结果(有可能是一个非法结果); 'isTransitionalDifferent' 键,布尔值,指示使用 UTS #46 的过滤机制是否会改变结果; 'errors' 键,是 IDNA_ERROR_* 常量集里一个常量对应的 int

返回值

UTF-8 编码的 Unicode 域名, 或者在失败时返回 false

更新日志

版本 说明
7.4.0 现在 variant 的默认值为 INTL_IDNA_VARIANT_UTS46 , 而不是已弃用的 INTL_IDNA_VARIANT_2003
7.2.0 INTL_IDNA_VARIANT_2003 已被弃用; 可以使用 INTL_IDNA_VARIANT_UTS46 代替。

示例

示例 #1 idn_to_utf8() 示例

<?php

echo idn_to_utf8('xn--tst-qla.de');

?>

以上示例会输出:

täst.de

参见

add a note

User Contributed Notes 1 note

up
8
kushik.com
10 years ago
<?php
// for those who has PHP older than version 5.3
class IDN {
// adapt bias for punycode algorithm
private static function punyAdapt(
$delta,
$numpoints,
$firsttime
) {
$delta = $firsttime ? $delta / 700 : $delta / 2;
$delta += $delta / $numpoints;
for (
$k = 0; $delta > 455; $k += 36)
$delta = intval($delta / 35);
return
$k + (36 * $delta) / ($delta + 38);
}

// translate character to punycode number
private static function decodeDigit($cp) {
$cp = strtolower($cp);
if (
$cp >= 'a' && $cp <= 'z')
return
ord($cp) - ord('a');
elseif (
$cp >= '0' && $cp <= '9')
return
ord($cp) - ord('0')+26;
}

// make utf8 string from unicode codepoint number
private static function utf8($cp) {
if (
$cp < 128) return chr($cp);
if (
$cp < 2048)
return
chr(192+($cp >> 6)).chr(128+($cp & 63));
if (
$cp < 65536) return
chr(224+($cp >> 12)).
chr(128+(($cp >> 6) & 63)).
chr(128+($cp & 63));
if (
$cp < 2097152) return
chr(240+($cp >> 18)).
chr(128+(($cp >> 12) & 63)).
chr(128+(($cp >> 6) & 63)).
chr(128+($cp & 63));
// it should never get here
}

// main decoding function
private static function decodePart($input) {
if (
substr($input,0,4) != "xn--") // prefix check...
return $input;
$input = substr($input,4); // discard prefix
$a = explode("-",$input);
if (
count($a) > 1) {
$input = str_split(array_pop($a));
$output = str_split(implode("-",$a));
} else {
$output = array();
$input = str_split($input);
}
$n = 128; $i = 0; $bias = 72; // init punycode vars
while (!empty($input)) {
$oldi = $i;
$w = 1;
for (
$k = 36;;$k += 36) {
$digit = IDN::decodeDigit(array_shift($input));
$i += $digit * $w;
if (
$k <= $bias) $t = 1;
elseif (
$k >= $bias + 26) $t = 26;
else
$t = $k - $bias;
if (
$digit < $t) break;
$w *= intval(36 - $t);
}
$bias = IDN::punyAdapt(
$i-$oldi,
count($output)+1,
$oldi == 0
);
$n += intval($i / (count($output) + 1));
$i %= count($output) + 1;
array_splice($output,$i,0,array(IDN::utf8($n)));
$i++;
}
return
implode("",$output);
}

public static function
decodeIDN($name) {
// split it, parse it and put it back together
return
implode(
".",
array_map("IDN::decodePart",explode(".",$name))
);
}

}
echo
IDN::decodeIDN($_SERVER['HTTP_HOST']);
?>
To Top