处理Emoji表情(unicode)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/EI__Nino/article/details/49150781

处理Emoji表情(unicode)

适用于Android 和 IOS

 /**
 * @brief 干掉emoji
 * @autho [email protected]
 * @param {String} $strText
 * @return {String} removeEmoji
 **/
static function escapeEmoji ($strText,$bool = false) {
    $preg =  '/\\\ud([8-9a-f][0-9a-z]{2})/i';
    if ($bool == true) {
        $boolPregRes = (preg_match($preg,json_encode($strText,true)));
        return $boolPregRes;
    } else {
        $strPregRes = (preg_replace($preg,'',json_encode($strText,true)));
        $strRet = json_decode($strPregRes,true);
        return $strRet;
    }
 }

原理:

PHP的正则无法解析 D800 之后的 unicode 字符
而一般的emoji都是在 D800到 D8FF 之间 (中文是:4E00-9FBF)

这样写

$preg = '/[\x{D800}-\x{D8FF}]+/iu'; 

是无效的。

所以只能通过json_encode转成unicode,再通过正则来处理Unicode字符,最后再转回普通字符串

猜你喜欢

转载自blog.csdn.net/EI__Nino/article/details/49150781