php 过滤emoji表情

function yz_expression()
{
		foreach ($_POST as $key => &$value) {
		$value = preg_replace_callback('/[xf0-xf7].{3}/', function($r) { return '@E' . base64_encode($r[0]);},$value);

			$countt=substr_count($value,"@");
			for ($i=0; $i < $countt; $i++) {
				$c = stripos($value,"@");
				$value=substr($value,0,$c).substr($value,$c+10,strlen($value)-1);
			}
			$value = preg_replace_callback('/@E(.{6}==)/', function($r) {return base64_decode($r[1]);}, $value);

		}
		return $_POST;
}
/**
     * 替换掉数组中的emoji表情
     * @param $arrayString
     * @param string $replaceTo
     * @return mixed|string
     */
    public static function filterEmojiDeep($arrayString,$replaceTo = '?')
    {
        if(is_string($arrayString))
        {
            return self::filterEmoji($arrayString,$replaceTo);
        }
        else if(is_array($arrayString))
        {
            foreach($arrayString as &$array)
            {
                if(is_array($array) || is_string($array))
                {
                    $array = self::filterEmojiDeep($array,$replaceTo);
                }
                else
                {
                    $array = $array;
                }
            }
        }
        return $arrayString;
    }

/**
     * 替换掉emoji表情
     * @param $text
     * @param string $replaceTo
     * @return mixed|string
     */
    public static function filterEmoji($text, $replaceTo = '?')
    {
        $clean_text = "";
        // Match Emoticons
        $regexEmoticons = '/[x{1F600}-x{1F64F}]/u';
        $clean_text = preg_replace($regexEmoticons, $replaceTo, $text);
        // Match Miscellaneous Symbols and Pictographs
        $regexSymbols = '/[x{1F300}-x{1F5FF}]/u';
        $clean_text = preg_replace($regexSymbols, $replaceTo, $clean_text);
        // Match Transport And Map Symbols
        $regexTransport = '/[x{1F680}-x{1F6FF}]/u';
        $clean_text = preg_replace($regexTransport, $replaceTo, $clean_text);
        // Match Miscellaneous Symbols
        $regexMisc = '/[x{2600}-x{26FF}]/u';
        $clean_text = preg_replace($regexMisc, $replaceTo, $clean_text);
        // Match Dingbats
        $regexDingbats = '/[x{2700}-x{27BF}]/u';
        $clean_text = preg_replace($regexDingbats, $replaceTo, $clean_text);
        return $clean_text;
    }
原文地址:https://www.cnblogs.com/-mrl/p/5704112.html