Search code examples
phpregexpcre

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


Solution

  • 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