如何在PHP中分割汉字? [英] How to split Chinese characters in PHP?
本文介绍了如何在PHP中分割汉字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一些关于如何在PHP中混合使用英语单词和数字的中文字符的帮助。
I need some help regarding how to split Chinese characters mixed with English words and numbers in PHP.
例如,如果我阅读
FrontPage 2000中文版應用大全
我希望得到
FrontPage, 2000, 中,文,版,應,用,大,全
或
FrontPage, 2,0,0,0, 中,文,版,應,用,大,全
如何实现这个?
提前感谢:)
推荐答案
假设您使用的是UTF-8(或者您可以使用Iconv或其他工具将其转换为UTF-8),然后使用 u
doc: http://www.php.net/manual/en /reference.pcre.pattern.modifiers.php )
Assuming you are using UTF-8 (or you can convert it to UTF-8 using Iconv or some other tools), then using the u
modifier (doc: http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php )
<?
$s = "FrontPage 2000中文版應用大全";
print_r(preg_match_all('/./u', $s, $matches));
echo "\n";
print_r($matches);
?>
会给予
21
Array
(
[0] => Array
(
[0] => F
[1] => r
[2] => o
[3] => n
[4] => t
[5] => P
[6] => a
[7] => g
[8] => e
[9] =>
[10] => 2
[11] => 0
[12] => 0
[13] => 0
[14] => 中
[15] => 文
[16] => 版
[17] => 應
[18] => 用
[19] => 大
[20] => 全
)
)
请注意,我的源代码存储在以UTF-8编码的文件中,以便$ s包含这些字符。
Note that my source code is stored in a file encoded in UTF-8 also, for the $s to contain those characters.
字母数字组:
<?
$s = "FrontPage 2000中文版應用大全";
print_r(preg_match_all('/(\w+)|(.)/u', $s, $matches));
echo "\n";
print_r($matches[0]);
?>
结果:
10
Array
(
[0] => FrontPage
[1] =>
[2] => 2000
[3] => 中
[4] => 文
[5] => 版
[6] => 應
[7] => 用
[8] => 大
[9] => 全
)
这篇关于如何在PHP中分割汉字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文