Php - regular expression to check if the string has chinese chars

孤街浪徒 提交于 2019-12-17 05:40:56

问题


I have the string $str and I want to check if it`s content has Chinese chars or not (true/false)

$str = "赕就可消垻,只有当所有方块都被消垻时才可以过关";

can you please help me?

Thanks! Adrian


回答1:


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.




回答2:


@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




回答3:


This link to a previous question on identifying simplified or traditional Chinese might give you some ideas... you don't actually specify which you mean, and I don't know Chinese well enough to recognise the difference




回答4:


preg_match("/^\p{Han}{2,10}+$/u", $str);

Use /^\p{Han}{2,10}+$/u regex which allows Chinese character only.

  1. It allows chinese character only &
  2. It allows Minimum 2 character &
  3. 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.



来源:https://stackoverflow.com/questions/4923319/php-regular-expression-to-check-if-the-string-has-chinese-chars

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