PHP轉換emoji表情為HTML字元實體

來源:互聯網
上載者:User

標籤:php emoji html字元實體

移動端輸入時允許輸入表情,表情實際上是UTF-16編碼,在資料庫儲存上會存在丟失,當前如果把資料庫字碼編碼修改為 utf8mb4 也是可以儲存。


如果不想修改其它則可以通過轉換把這些Emoji轉換為HTML字元實體儲存。

代碼如下:


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;}


注意:這裡字元儲存為 utf-8 如果代碼所處理的格式為 GBK 自行修改。


本文出自 “秋風掃落葉” 部落格,請務必保留此出處http://php2012web.blog.51cto.com/5585213/1863313

PHP轉換emoji表情為HTML字元實體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.