微信公众号解析和发送emoji表情

对微信发送的emoji表情进行转义,并将emoji表情转移为微信能够理解的格式

function testEmoji(){ $r = ''; // 解析服务器发送过来的表情消息 $tmpStr = json_encode($this->getRequest("content")); $r .= "msg after encode {$tmpStr}\n"; $tmpStr = preg_replace("#(\\\ue[0-9a-f]{3})#ie", "addslashes('\\1')", $tmpStr); $r .= "msg after replace {$tmpStr}\n"; $tmpStr = json_decode($tmpStr); $r = $r . "user send to server: {$tmpStr}"; // 将表情文本格式转化为编码格式 $back_str = $this->unicode2utf8_2("\ue159")."【公交/地铁】"; $this->responseText($r . "\nserver send to user: " . $back_str); }


function parseHtmlemoji ($text)
{
    require_once 'emoji/emoji.php';
    $tmpStr = json_encode($text);
    $tmpStr = preg_replace("#(\\\ue[0-9a-f]{3})#ie", "addslashes('\\1')", 
    $tmpStr);
    $text = json_decode($tmpStr);
    preg_match_all("#u([0-9a-f]{4})+#iUs", $text, $rs);
    if (empty($rs[1])) {
        return $text;
    }
    foreach ($rs[1] as $v) {
        $test_iphone = '0x' . trim(strtoupper($v));
        $test_iphone = $test_iphone + 0;
        $t = emoji_unified_to_html(
        emoji_softbank_to_unified(utf8_bytes($test_iphone)));
        $text = str_replace("\u$v", $t, $text);
    }
    return $text;
}
function utf8_bytes ($cp)
{
    if ($cp > 0x10000) {
        # 4 bytes
        return chr(0xF0 | (($cp & 0x1C0000) >> 18)) .
         chr(0x80 | (($cp & 0x3F000) >> 12)) .
         chr(0x80 | (($cp & 0xFC0) >> 6)) . chr(0x80 | ($cp & 0x3F));
    } else
        if ($cp > 0x800) {
            # 3 bytes
            return chr(0xE0 | (($cp & 0xF000) >> 12)) .
             chr(0x80 | (($cp & 0xFC0) >> 6)) . chr(0x80 | ($cp & 0x3F));
        } else
            if ($cp > 0x80) {
                # 2 bytes
                return chr(0xC0 | (($cp & 0x7C0) >> 6)) .
                 chr(0x80 | ($cp & 0x3F));
            } else {
                # 1 byte
                return chr($cp);
            }
}
 
posted @ 2015-10-27 14:18  王者杂货铺  阅读(1304)  评论(0)    收藏  举报