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?

1

2 Answers 2

12

I've ended up implementing it like this:

$utf8 = html_entity_decode("一", ENT_COMPAT, 'UTF-8');
Sign up to request clarification or add additional context in comments.

2 Comments

use ENT_QUOTES | ENT_COMPAT to convert quotes as well
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")
3
function hexToString($str){return chr(hexdec(substr($str, 2)));}
$result = preg_replace_callback("/(\\\\x..)/isU", function($m) { return hexToString($m[0] ); }, $str);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.