Search code examples
phpregexvalidationutf-8non-latin

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 and 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.


Solution

  • 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
    }