开发者

check if string contains any character other than what's allowed

开发者 https://www.devze.com 2023-02-28 17:11 出处:网络
In username - I want to only allow alphanumeric characters and 10 other U开发者_如何转开发TF8 characters that I want. What would be a proper way to do this? Would UTF8 be a problem with preg functions

In username - I want to only allow alphanumeric characters and 10 other U开发者_如何转开发TF8 characters that I want. What would be a proper way to do this? Would UTF8 be a problem with preg functions and do I need to use another method?

Thanks


I recently asked how to make this Just Do The Right Thing, and learned that it’s rather prickly to get everything quite right.

If you can’t get /\w/u to work right for you on Unicode, it is mostly like /[\pL\pM\p{Nd}\p{Nl}\p{Pc}]/u.


For proper Unicode support (including proper case-awareness) you need to use the '/u' modifier IIRC. But, but, but: be aware that most other PHP functions will treat PHP strings as byte strings (not character strings), which means if you're doing stuff like stripos() you will get offsets in byte indices and not character indices, and they will not work reliably for malformed UTF-8 (because they match byte sequences rather than character sequences).

Also, note that UTF-8 literals may not be such a good idea if you need to run this script somewhere that doesn't use UTF-8 as system locale (Windows).


Preg is your best bet. Do something like this replacing the random japanese characters, I used, with your choice of allowed characters.

if (preg_match('/[\x{0030}-\x{0039}\x{0041}-\x{005A}\x{0061}\-u007A]+/u', $subject)) {
return true;
}
return false;

If you need help with regex expressions then I recomend regexbuddy, which you can get at regexbuddy.com

0

精彩评论

暂无评论...
验证码 换一张
取 消