Creating a UTF-8 string from hexadecimal code
Asked Answered
P

2

3

In C++, it's possible create a UTF-8 string using this kind of notation: "\uD840\uDC50".

However this doesn't work in PHP. Is there a similar notation?

If not, is there any built-in way to create a UTF-8 string knowing its Unicode code point?

Pluton answered 19/4, 2013 at 6:44 Comment(1)
php.net/manual/en/function.chr.php#88611Rustle
P
12

I've ended up implementing it like this:

$utf8 = html_entity_decode("一", ENT_COMPAT, 'UTF-8');
Pluton answered 19/4, 2013 at 6:57 Comment(2)
use ENT_QUOTES | ENT_COMPAT to convert quotes as wellMoser
This has limitations and will not work with all UTF-8 chars, as not all hex chars are suported in HTML standard. See ascii.cl/htmlcodes.htm ("not defined in HTML 4 standard")Fourlegged
Y
3
function hexToString($str){return chr(hexdec(substr($str, 2)));}
$result = preg_replace_callback("/(\\\\x..)/isU", function($m) { return hexToString($m[0] ); }, $str);
Yawl answered 23/2, 2015 at 10:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.