Convert & # 55357; & # 56911; to emoji in HTML using PHP

We have a bunch of surrogate pairs (or 2-byte utf8?) Characters, such as �� which are emojis prayer hands stored as UTF8 as 2 characters. When rendering in the browser, this line is displayed as two?

example: 🙏

I need to convert them to emjoi hands using php, but I just can't find a combination of iconv, utf8_decode, html_entity_decode etc. to disable it.

This site correctly converts �� :

http://www.convertstring.com/EncodeDecode/HtmlDecode

Insert the following line there

Please join me in this prayer. ��❤️

You will notice that the pair of supragates 🙏 ( �� ) is converted to 🙏

This site requires the use of HTMLDecode, but I cannot find anything inside php to remove this. I tried: Iconv html_entity_decode and several public libraries.

I admit that I am not an expert when it comes to converting characters around!

+4
source share
1 answer

I was unable to find a function for this, but this works:

$str = "Please join me in this prayer. ��❤️"; $newStr = preg_replace_callback("/&#.....;&#.....;/", function($matches){return convertToEmoji($matches);}, $str); print_r($newStr); function convertToEmoji($matches){ $newStr = $matches[0]; $newStr = str_replace("&#", '', $newStr); $newStr = str_replace(";", '##', $newStr); $myEmoji = explode("##", $newStr); $newStr = dechex($myEmoji[0]) . dechex($myEmoji[1]); $newStr = hex2bin($newStr); return iconv("UTF-16BE", "UTF-8", $newStr); }

+1
source

Source: https://habr.com/ru/post/1274566/


All Articles