1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <?php
$str = 'Am\u00e9lie';
$str = str_replace('\u00', '\x', $str);
print preg_replace_callback('/\\\xe9/', 'cp2utf8', $str);
function cp2utf8 ($hexcp) {
$outputString = '';
$n = hexdec($hexcp[0]);
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;
} |