Home > Backend Development > PHP Problem > How to convert php emoji to text

How to convert php emoji to text

藏色散人
Release: 2023-03-13 14:34:01
Original
7420 people have browsed it

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.

How to convert php emoji to text

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]) && (&#39;0x&#39; . $arr[$n] >= 0xd800 && &#39;0x&#39; . $arr[$n] <= 0xdbff && &#39;0x&#39; . $arr[$n + 1] >= 0xdc00 && &#39;0x&#39; . $arr[$n + 1] <= 0xdfff)) {
            $H = &#39;0x&#39; . $arr[$n];
            $L = &#39;0x&#39; . $arr[$n + 1];
            $code = ($H - 0xD800) * 0x400 + 0x10000 + $L - 0xDC00;
            $str.= &#39;&#&#39; . $code . &#39;;&#39;;
            $n++;
        } else {
            $str.=mb_convert_encoding(hex2bin($arr[$n]),&#39;utf-8&#39;,&#39;utf-16&#39;);
        }
    }
    return $str;
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template