Using Arabic characters with ctype_alnum

[亡魂溺海] 提交于 2019-12-20 02:54:49

问题


I need to allow Arabic usernames on my website which is already using ctype_alnum to validate the username field. When I try to use Arabic usernames, the validation error message is returned. ctype_alnum does not recognize arabic characters as letters and numbers. How can i work around this ?


回答1:


You can find all Arabic Characters by using this Regex:

preg_match("/^[a-zA-Z\p{Cyrillic}0-9\s\-]+$/u", $string);

If the matched length equals the username length it is an arabic username.




回答2:


ctype_alnum only recognizes 0-9A-Za-z.

You can either use regular expressions (as crothhass posted while I was writing this), or you can attempt to convert Arabic into Latin alphabet, check this with ctype_alnum, then convert it back again.

But what I actually recommend is to look at the problem from the other direction, and just check for characters that you DON'T want. This is probably spaces and some punctuation, since you are likely using UTF-8 anyway and can accept anything else.



来源:https://stackoverflow.com/questions/8153002/using-arabic-characters-with-ctype-alnum

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!