The following code converts a hex codepoint to a sequence of bytes that represent the Unicode codepoint in UTF-8.
This is useful because PHP’s chr() function only works on ASCII
(.
function cp2utf8 ($hexcp) {
$outputString = '';
$n = hexdec($hexcp);
if ($n < = 0x7F) {
$outputString .= chr($n);
}
else if ($n <= 0x7FF) {
$outputString .= chr(0xC0 | (($n>>6) & 0x1F))
.chr(0x80 | ($n & 0x3F));
}
else if ($n < = 0xFFFF) {
$outputString .= chr(0xE0 | (($n>>12) & 0x0F))
.chr(0x80 | (($n>>6) & 0x3F))
.chr(0x80 | ($n & 0x3F));
}
else if ($n < = 0x10FFFF) {
$outputString .= chr(0xF0 | (($n>>18) & 0x07))
.chr(0x80 | (($n>>12) & 0x3F)).chr(0x80 | (($n>>6) & 0x3F))
.chr(0x80 | ($n & 0x3F));
}
else {
$outputString .= 'Error: ' + $n +' not recognised!';
}
return $outputString;
}
