Detect chinese (multibyte) character in the string

≡放荡痞女 提交于 2019-12-18 01:08:16

问题


$str = "This is a string containing 中文 characters. Some more characters - 中华人民共和国 ";

How do I detect chinese characters from this string and print the part which starts with the first character and ends with "-"? (it would be "中文 characters. Some more characters -").

Thank you!


回答1:


I've solved this problem using preg_match and regular expressions:

$str = "This is a string containing 中文 characters. Some more characters - 中华人民共和国 ";

preg_match(/[\x{4e00}-\x{9fa5}]+.*\-/u, $str, $matches);



回答2:


Is PHP storing this as Unicode? If so, at worst you could step through the string, character by character, until you hit those within the Chinese range.

Check this out too PHP: Unicode - Manual



来源:https://stackoverflow.com/questions/1550950/detect-chinese-multibyte-character-in-the-string

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