检测字符串中的中文(多字节)字符 [英] Detect chinese (multibyte) character in the string

查看:140
本文介绍了检测字符串中的中文(多字节)字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$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 -").

谢谢!

推荐答案

我已经使用preg_match和正则表达式解决了这个问题:

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);

这篇关于检测字符串中的中文(多字节)字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆