开发者

Regex validation on UTF8 / multi byte 'language' characters (inc chinese etc) but not special characters such as {/*

Using PHP / MySQL all encoded up as UTF, we have recently had to start capturing non-Latin characters, such as Chinese etc. We have PHP validation that checks the string length a开发者_运维技巧nd alpha numeric such as:

if (!ereg("[[:alnum:]]{2,}",$_POST['company_name'])) {
    //error code here
}

This is not working on multi byte chars. I understand about the length being an issue (one char is not equal to one byte) but I was hoping if someone could provide a link / solution for matching a string for UTF8 language characters only NO special characters such as [*/ etc.

EDIT: I want to accept only a string that is xx long and only contains language characters alebit English / Chinese etc. and NOT any special characters *{/ etc. Hopefully that clarifies.


Your requirements are a little vague, but you can enforce only letters (possibly combined with marks) and decimal numbers with

if (!preg_match('/^[\p{L}\p{M}\p{Nd}]{2,}$/u', $_POST['company_name'])) {
   //error here
}


The mbstring extension of PHP has an mb_ereg() function, this would probably be a good starting point, I guess.


You can try to match with \p{L}|\p{N} but you need to add the u option to your regex.

Sources :
www.regular-expressions.info

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜