Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Unicode codepoint to character

Tags:

php

unicode

I would like to convert Unicode codepoint to character. Here is what I have tried:

$point = dechex(127468);  // 1f1ec

echo "\u{1f1ec}";         // this works
echo "\u{$point}";        // this outputs '\u1f1ec'
echo "\u{{$point}}";      // Parse error: Invalid UTF-8 codepoint escape sequence
echo "\u\{{$point}\}";    // outputs \u\{1f1ec\}
echo "\u{". $point ."}";  // Parse error; same as above
like image 807
jakub_jo Avatar asked Jan 24 '18 21:01

jakub_jo


People also ask

How to detect Unicode characters in PHP?

In PHP, we can use the mb_ord() function to get the Unicode code point value of a given character. This function is supported in PHP 7 or higher versions. The mb_ord() function complements the mc_chr() function.

Does PHP support Unicode?

PHP 7.0. 0 has introduced the "Unicode codepoint escape" syntax. It's now possible to write Unicode characters easily by using a double-quoted or a heredoc string, without calling any function.

What is UTF-8 PHP?

Definition and Usage. The utf8_encode() function encodes an ISO-8859-1 string to UTF-8. Unicode is a universal standard, and has been developed to describe all possible characters of all languages plus a lot of symbols with one unique number for each character/symbol.


1 Answers

You don't need to convert integer to hexadecimal string, instead use IntlChar::chr:

echo IntlChar::chr(127468);

Directly from docs of IntlChar::chr:

Return Unicode character by code point value

like image 123
Aniket Sahrawat Avatar answered Nov 02 '22 07:11

Aniket Sahrawat