<?php
class IDN {
private function adaptBias($delta, $numPoints, $first)
{
$delta = $first ? $delta / 700 : $delta / 2;
$delta += $delta / $numPoints;
for ($k = 0; $delta > 455; $k += 36) {
$delta = (int)($delta / 35);
}
return $k + (36 * $delta) / ($delta + 38);
}
private function encodeUtf8CodePoint($codePoint)
{
switch (true) {
case $codePoint < 0x80:
return pack('C*', $codePoint & 0x7F);
case $codePoint < 0x0800:
return pack('C*', (($codePoint & 0x07C0) >> 6) | 0xC0, ($codePoint & 0x3F) | 0x80);
case $codePoint < 0x010000:
return pack('C*', (($codePoint & 0xF000) >> 12) | 0xE0, (($codePoint & 0x0FC0) >> 6) | 0x80, ($codePoint & 0x3F) | 0x80);
case $codePoint < 0x110000:
return pack('C*', (($codePoint & 0x1C0000) >> 18) | 0xF0, (($codePoint & 0x03F000) >> 12) | 0x80, (($codePoint & 0x0FC0) >> 6) | 0x80, ($codePoint & 0x3F) | 0x80);
}
return false;
}
public function decodePart($input) {
if (substr($input, 0, 4) !== 'xn--') {
return $input;
}
$input = substr($input, 4);
if (false !== $nonBasicCharsStart = strrpos($input, '-')) {
$output = str_split(substr($input, 0, $nonBasicCharsStart), 1);
$nonBasicChars = substr($input, $nonBasicCharsStart + 1);
} else {
$output = [];
$nonBasicChars = $input;
}
$n = 128;
$i = 0;
$bias = 72;
for ($j = 0, $l = strlen($nonBasicChars); $j < $l; $i++) {
$oldi = $i;
$w = 1;
$k = 36;
while (true) {
if (!isset($nonBasicChars[$j])) {
return false;
}
$digit = ord($nonBasicChars[$j++]);
if ($digit >= 0x61 && $digit <= 0x7A) {
$digit -= 97;
} else if ($digit >= 0x30 && $digit <= 0x39) {
$digit -= 22;
} else {
return false;
}
$i += $digit * $w;
if ($k <= $bias) {
$t = 1;
} else if ($k >= $bias + 26) {
$t = 26;
} else {
$t = $k - $bias;
}
if ($digit < $t) {
break;
}
$w *= (int) (36 - $t);
$k += 36;
}
$c = count($output) + 1;
$bias = $this->adaptBias($i - $oldi, $c, $oldi === 0);
$n += (int)($i / $c);
$i %= $c;
array_splice($output, $i, 0, array($this->encodeUtf8CodePoint($n)));
}
return implode('', $output);
}
public function decodeIDN($name) {
// split it, parse it and put it back together
return
implode(
".",
array_map([$this, 'decodePart'],explode(".",$name))
);
}
}
echo (new IDN)->decodeIDN("xn---with-SUPER-MONKEYS-pc58ag80a8qai00g7n9n");
- Output for git.master, git.master_jit, rfc.property-hooks
- 安室奈美恵-with-SUPER-MONKEYS
This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches
Archived branches
Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:
53.49 ms | 401 KiB | 8 Q