简体   繁体   中英

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. 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. So, regular ASCII digits will not be removed.

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}". Key Ten is the unicode symbol "\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:

123🗡️abcx8x🗡️56

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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