28

I use this table of Emoji and try this code:

<?php print json_decode('"\u2600"'); // This convert to ☀ (black sun with rays) ?>

If I try to convert this \u1F600 (grinning face) through json_decode, I see this symbol — ὠ0.

Whats wrong? How to get right Emoji?

Platon
  • 495
  • 1
  • 4
  • 11
  • 2
    Did you try the bytes notation? [echo "\xF0\x9F\x98\x80";](https://eval.in/463740) Maybe your browser can't display this one? – bobble bubble Nov 05 '15 at 14:51
  • If you save the PHP file in the correct encoding you can also just write `print('');`. – roeland Nov 05 '15 at 20:23
  • If you problem is related to how to save them in database if you are using MySQL you can change the chartset to `utfbm4`. Take a look to [here](https://github.com/yiisoft/yii2/issues/16576). – Juan Antonio Oct 21 '19 at 11:50

3 Answers3

56

PHP 5

JSON's \u can only handle one UTF-16 code unit at a time, so you need to write the surrogate pair instead. For U+1F600 this is \uD83D\uDE00, which works:

echo json_decode('"\uD83D\uDE00"');

PHP 7

You now no longer need to use json_decode and can just use the \u and the unicode literal:

echo "\u{1F30F}";

Jimbo
  • 25,790
  • 15
  • 86
  • 131
Tino Didriksen
  • 2,215
  • 18
  • 21
7

In addition to the answer of Tino, I'd like to add code to convert hexadecimal code like 0x1F63C to a unicode symbol in PHP5 with splitting it to a surrogate pair:

function codeToSymbol($em) {
    if($em > 0x10000) {
        $first = (($em - 0x10000) >> 10) + 0xD800;
        $second = (($em - 0x10000) % 0x400) + 0xDC00;
        return json_decode('"' . sprintf("\\u%X\\u%X", $first, $second) . '"');
    } else {
        return json_decode('"' . sprintf("\\u%X", $em) . '"');
    }
}

echo codeToSymbol(0x1F63C); outputs

shukshin.ivan
  • 11,075
  • 4
  • 53
  • 69
0

Example of code parsing string including emoji unicode format

$str = 'Test emoji \U0001F607 \U0001F63C';

echo preg_replace_callback(
    '/\\\U([A-F0-9]+)/',
    function ($matches) {
        return mb_convert_encoding(hex2bin($matches[1]), 'UTF-8', 'UTF-32');
    },
    $str
);

Output: Test emoji

https://3v4l.org/63dUR

psk
  • 1
  • 3
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 22 '22 at 21:26