php 过滤emoji表情

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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;
    }
复制代码

 

posted @   一个人的孤独自白  阅读(6385)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
点击右上角即可分享
微信分享提示