private static function dec2char($n) { // converts a single string representing a decimal number to a character // note that no checking is performed to ensure that this is just a hex number, eg. no spaces etc // dec: string, the dec codepoint to be converted $result = ''; if ($n <= 0xffff) { $result .= Z_Unicode::fromCharCode($n); } else { if ($n <= 0x10ffff) { $n -= 0x10000; $result .= Z_Unicode::fromCharCode(0xd800 | $n >> 10) . Z_Unicode::fromCharCode(0xdc00 | $n & 0x3ff); } else { throw new Exception('dec2char error: Code point out of range: ' . dechex($n)); } } return $result; }
private static function utf8_decode($utftext) { $string = ""; $i = 0; $c = $c1 = $c2 = 0; while ($i < mb_strlen($utftext)) { $c = Z_Unicode::charCodeAt($utftext, $i); if ($c < 128) { $string .= Z_Unicode::fromCharCode($c); $i++; } else { if ($c > 191 && $c < 224) { $c2 = Z_Unicode::charCodeAt($utftext, $i + 1); $string .= Z_Unicode::fromCharCode(($c & 31) << 6 | $c2 & 63); $i += 2; } else { $c2 = Z_Unicode::charCodeAt($utftext, $i + 1); $c3 = Z_Unicode::charCodeAt($utftext, $i + 2); $string .= Z_Unicode::fromCharCode(($c & 15) << 12 | ($c2 & 63) << 6 | $c3 & 63); $i += 3; } } } return $string; }