ivoba
ivoba

Reputation: 5986

Convert emoji code into emoticon character in PHP

How can i convert this string

U+1F600

into the resp. emoticon or emoji?

I can do this which works fine:

echo "\u{1F600}";

But when i try to convert a string containing U+1F600 into this literal it does not work.

F.e this will yield in

Invalid UTF-8 codepoint escape sequence

$str = "Hello U+1F600";
preg_match("/U\+[0-9a-f]{4,5}/mi", $str, $find);
$text = str_replace($find[0], "\u{".str_replace("U+","", $find[0])."}", $str);

echo $text;

So what can i do with the Emoji code in PHP to get the Emoji character?

Upvotes: 3

Views: 3532

Answers (1)

Romain B.
Romain B.

Reputation: 654

You can do a preg_replace() :

$str = "Hello U+1F600";
$str = preg_replace("/U\+([0-9a-f]{4,5})/mi", '&#x${1}', $str);
echo $str;

It will display

Hello 😀

Upvotes: 0

Related Questions