PHP:编写一个简单的removeEmoji函数 [英] PHP : writing a simple removeEmoji function

查看:92
本文介绍了PHP:编写一个简单的removeEmoji函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个简单的功能,可以从instagram评论中删除表情符号字符.我现在已经尝试过的内容(带有在SO和其他网站上找到的示例中的许多代码):

I'm looking for a simple function that would remove Emoji characters from instagram comments. What I've tried for now (with a lot of code from examples I found on SO & other websites) :

// PHP class
public static function removeEmoji($string)
{
    // split the string into UTF8 char array
    // for loop inside char array
        // if char is emoji, remove it
    // endfor
    // return newstring
}

任何帮助将不胜感激

推荐答案

我认为preg_replace函数是最简单的解决方案.

I think the preg_replace function is the simpliest solution.

根据 EaterOfCode 的建议,我阅读了

As EaterOfCode suggests, I read the wiki page and coded new regex since none of SO (or other websites) answers seemed to work for Instagram photo captions (API returning format) . Note: /u identifier is mandatory to match \x unicode chars.

public static function removeEmoji($text) {

    $clean_text = "";

    // Match Emoticons
    $regexEmoticons = '/[\x{1F600}-\x{1F64F}]/u';
    $clean_text = preg_replace($regexEmoticons, '', $text);

    // Match Miscellaneous Symbols and Pictographs
    $regexSymbols = '/[\x{1F300}-\x{1F5FF}]/u';
    $clean_text = preg_replace($regexSymbols, '', $clean_text);

    // Match Transport And Map Symbols
    $regexTransport = '/[\x{1F680}-\x{1F6FF}]/u';
    $clean_text = preg_replace($regexTransport, '', $clean_text);

    // Match Miscellaneous Symbols
    $regexMisc = '/[\x{2600}-\x{26FF}]/u';
    $clean_text = preg_replace($regexMisc, '', $clean_text);

    // Match Dingbats
    $regexDingbats = '/[\x{2700}-\x{27BF}]/u';
    $clean_text = preg_replace($regexDingbats, '', $clean_text);

    return $clean_text;
}

该功能不会删除所有表情符号,因为还有更多表情符号,但是您明白了.

The function does not remove all emojis since there are many more, but you get the point.

请参考 unicode.org-完整的表情符号列表(感谢 Epoc )

这篇关于PHP:编写一个简单的removeEmoji函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆