Php - regular expression to check if the string has chinese chars
You could use a unicode character class http://www.regular-expressions.info/unicode.html
preg_match("/\p{Han}+/u", $utf8_str);
This just checks for the presence of at least one chinese character. You might want to expand on this if you want to match the complete string.
@mario
answer is right!
For Chinese chars use this regex: /[\x{4e00}-\x{9fa5}]+/u
And Don't forget the u
modifier!!!
About u
modifier reference
TKS to mario
preg_match("/^\p{Han}{2,10}+$/u", $str);
Use /^\p{Han}{2,10}+$/u regex which allows Chinese character only.
- It allows chinese character only &
- It allows Minimum 2 character &
- It allows maximum 10 character
You can change minimum and maximum character by changing {2,10} as per your need.
\p & /u are very important to add please don't avoid to add it.