Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a UTF-8 string from hexadecimal code

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?

like image 433
laurent Avatar asked Sep 26 '26 09:09

laurent


2 Answers

I've ended up implementing it like this:

$utf8 = html_entity_decode("一", ENT_COMPAT, 'UTF-8');
like image 175
laurent Avatar answered Sep 27 '26 23:09

laurent


function hexToString($str){return chr(hexdec(substr($str, 2)));}
$result = preg_replace_callback("/(\\\\x..)/isU", function($m) { return hexToString($m[0] ); }, $str);
like image 34
Jiri Zachar Avatar answered Sep 27 '26 23:09

Jiri Zachar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!