首页 > 解决方案 > Convert "Fancy" unicode ABC to standard ABC

问题描述

I run Regex checks on certain inputs on my site, but the Regex wrongfully returns false when users use "Fancy" Unicode sets such as:

Ⓜⓐⓣⓒⓗ Match ⒨⒜⒯⒞⒣

These are not different fonts, they are different characters! None of these are matched by /Match/ (Proof)

How can I convert the user input to standard ABC characters before running through my Regex checks? (I'm using PHP, if that makes a difference)

标签: phpregexunicodepreg-matchspecial-characters

解决方案


NFKD unicode规范化应该处理其中的大部分。但是,它似乎只有intl在启用模块时才有效,而且我的环境中没有它,所以我无法测试它。如果您也没有这样的 PHP,并且不想安装它,这有点类似,至少对于某些字符:

iconv("UTF-8", "ISO-8859-1//TRANSLIT", $text)

最后,您可以制作自己的映射,例如使用strtr(然后您将知道它可以工作,因为您自己编写了它)。


推荐阅读