dsk710351 2018-08-21 03:06
浏览 146
已采纳

PHP:如何匹配一系列unicode配对的代理表情符号/表情符号?

anubhava's answer about matching ranges of unicode characters led me to the regex to use for cleaning up a specific range of single code point of characters. With it, now I can match all miscellaneous symbols in this list (includes emoticons) with this simple expression:

preg_replace('/[\x{2600}-\x{26FF}]/u', '', $str);

However, I also want to match those in this list of paired/double surrogates emoji, but as nhahtdh explained in a comment:

There is a range from d800 to dfff to specify surrogates in UTF-16 to allow for more characters to be specified. A single surrogate is not a valid character in UTF-16 (a pair is necessary to specify a valid character).

So, for example, when I try this:

preg_replace('/\x{D83D}\x{DE00}/u', '', $str);

For replacing only the first of the paired surrogates on this list, i.e.:

  • 写回答

1条回答 默认 最新

  • douzhuo2722 2018-08-21 06:43
    关注

    revo's comment above was very helpful to find a solution:

    If your PHP isn't shipped with a PCRE build for UTF-16 then you can't perform such a match. From PHP 7.0 on, you're able to use Unicode code points following this syntax \u{XXXX} e.g. preg_replace("~\u{1F600}~", '', $str); (Mind the double quotes)

    Since I am using PHP 7, echo "\u{1F602}"; outputs

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部