删除表情符号时Regex与unicode不匹配';s来自电子邮件正文


Regex not matching unicode when removing emoji's from email body

更新

在使用:regex101之后,我发现它只匹配1个结果,当将修饰符更改为''g时,它会吓坏!:(-样品测试:https://regex101.com/r/yD9hI4/1

更新2

当在上面的示例中添加ug修饰符时,它(有点(起作用!但是PHP在Unknown modifier g中抛出了一个异常:(不能赢!讨厌regex…

更新3

从那以后,我读过preg_replace,但它不理解'g,因为当省略限制时,它默认为全部匹配(事实就是这样(。所以现在我真的不明白为什么preg_replace只找到一个匹配而忽略其余的

原始问题我试图删除表情符号/Unicode图片字符的所有引用,但我的regex与gmail不匹配。我已经下载了以下电子邮件进行处理:

Test
*Test*

*[image: Inline images 1]*
*Test?*
Test?
=F0=9F=98=88
TEST!

=F0=9F=98=88正是我想要摆脱的。在这个例子中,它是带角的微笑(谷歌风格(。它由unicode值表示:U+1F608(hex/utf-8:f0 9f 98 88(-现在,这是唯一不会消失的bug,其他一切都由我的regex覆盖:

/['x{1F600}-'x{1F64F}]/u(微笑(和/['x{1F680}-'x{1F6FF}]/u(运输等(

现在,在有人指出显而易见的问题并说:"=F0=9f=98=88不是你要搜索的"之前,我同意,但我想知道为什么当我用其他表情符号测试时,只有小恶魔能在我的解析中幸存下来?

有人(比我更懂正则表达式(能解释出了什么问题吗?

请求的代码

private function removePictureCharacters($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;
}

我有一个非常脆弱的破解,但它确实完成了任务:

private function removePictureCharacters($text) {
    $clean_text = "";
    // Never trust a horned smiler
    $smiley = json_decode('"'u1F608"');
    $clean_text = preg_replace("/$smiley/u", '', $text);
    // Match Emoticons
    $regexEmoticons = '/(['x{1F600}-'x{1F64F}])/u';
    ...
}