PHP-处理emoji表情
MySQL中的UTF8编码每个字符只支持最大3字节,而emoji是4个字节的,不能直接存储进去,所以,需要将emoji转换一下。
这边提供一个将4个字节的utf8编码转成&#xxxxxx;的方法:
$content = preg_replace_callback(‘/[\x{010000}-\x{10ffff}]/u‘, function($v){ return ‘&#‘.current(unpack(‘N‘,iconv(‘UTF-8‘, ‘UCS-4‘, $v[0]))).‘;‘; }, $content);
解码也简单,反过来一下就好了
$content = preg_replace_callback(‘/&#(\d+);/‘, function($v){ if ($v[1] >= 0x10000 && $v[1] <= 0x10ffff) { return iconv(‘UCS-4‘, ‘UTF-8‘, pack(‘N‘,$v[1])); } return $v[0]; }, $content);
相关推荐
ailxxiaoli 2020-11-16
唐文 2020-06-28
fujuan000 2020-04-30
Andrea0 2020-01-14
Trustport 2019-12-01
coinone 2019-10-29
boox 2018-10-25
TinyDolphin 2019-10-30
happinessaflower 2019-10-26
xuefeng0 2015-04-13
haixianTV 2015-08-30
yzhj00 2017-11-23
AxDoctor 2016-04-12
quanhaoH 2012-04-21
85206537 2015-09-19
88286530 2015-09-19
81246831 2015-08-24
chh 2015-05-05