简体   繁体   English

替换 PHP 中的 UTF-8 KeyCap 数字字符

[英]Replacing UTF-8 KeyCap Digit Character in PHP

I am trying to replace these [0️⃣1️⃣2️⃣2️⃣3️⃣4️⃣5️⃣6️⃣7️⃣8️⃣9️⃣] UTF characters from a string.我正在尝试从字符串中替换这些 [0️⃣1️⃣2️⃣2️⃣3️⃣4️⃣5️⃣6️⃣7️⃣8️⃣9️⃣] 个 UTF 字符。 But somehow all other digit characters are also getting replaced.但不知何故,所有其他数字字符也被替换了。 I've tried using replacing by range.我试过使用按范围替换。 Here is what i've tried这是我尝试过的

$post = '  🗡️7️⃣8️⃣6️⃣🗡️  ';

$post = preg_replace('/[\x{0030}-\x{0040}]/u', '', $post);

echo $post;

How to do it怎么做

You may remove all those digits that have diacritic marks after them (all those numbers you shared are actually digits with some diacritics after):您可以删除所有后面带有变音符号的数字(您共享的所有数字实际上都是后面带有一些变音符号的数字):

preg_replace('/[0-9]\p{M}+/u', '', $post)

The [0-9] will match ASCII digits from 0 to 9 , and the \p{M}+ matches 1 or more diacritic marks. [0-9]将匹配从09的 ASCII 数字,而\p{M}+匹配 1 个或多个变音符号。 So, regular ASCII digits will not be removed.因此,不会删除常规 ASCII 数字。

See the regex demo查看正则表达式演示

I'm not sure what result you need.我不确定你需要什么结果。

The key's one to nine are combinations how "5\u{fe0f}\u{20e3}".键的一到九是“5\u{fe0f}\u{20e3}”的组合。 Key Ten is the unicode symbol "\u{1f51f}".关键十是 unicode 符号“\u{1f51f}”。 If only these symbols are to be removed, you must do this:如果只删除这些符号,您必须这样做:

$post = '123🗡️0️⃣1️⃣2️⃣2️⃣3️⃣abc4️⃣x8x5️⃣6️⃣7️⃣8️⃣9️⃣🔟🗡️56';

$post = preg_replace('~[0-9]\x{fe0f}\x{20e3}|\x{1f51f}~u', '', $post);

echo $post;

Output: Output:

123🗡️abcx8x🗡️56

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM