Home >Backend Development >PHP Problem >How to convert php emoji to text
php How to convert emoji to text: 1. Create a PHP sample file; 2. Use the "function utf16_to_entities(){...}" method to convert the emoji into an HTML character entity and save it.
The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer
How to convert php emoji to text?
PHP converts emoji expressions into HTML character entities:
Allows expressions to be entered when inputting on the mobile terminal. The expressions are actually UTF-16 encoded and stored in the database There will be loss. Currently, if you change the database character encoding to utf8mb4, it can be saved.
If you don’t want to modify other emoticons, you can convert these emoticons into HTML character entities and save them.
The code is as follows:
function utf16_to_entities(){ $content = mb_convert_encoding($content, 'utf-16'); $bin = bin2hex($content); $arr = str_split($bin, 4); $l = count($arr); $str = ''; for ($n = 0; $n < $l; $n++) { if (isset($arr[$n + 1]) && ('0x' . $arr[$n] >= 0xd800 && '0x' . $arr[$n] <= 0xdbff && '0x' . $arr[$n + 1] >= 0xdc00 && '0x' . $arr[$n + 1] <= 0xdfff)) { $H = '0x' . $arr[$n]; $L = '0x' . $arr[$n + 1]; $code = ($H - 0xD800) * 0x400 + 0x10000 + $L - 0xDC00; $str.= '&#' . $code . ';'; $n++; } else { $str.=mb_convert_encoding(hex2bin($arr[$n]),'utf-8','utf-16'); } } return $str; }
Note: The characters here are saved as utf-8. If the format processed by the code is GBK, modify it yourself.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert php emoji to text. For more information, please follow other related articles on the PHP Chinese website!