Home  >  Article  >  Backend Development  >  中文分词 - php逐个汉字遍历字符串

中文分词 - php逐个汉字遍历字符串

WBOY
WBOYOriginal
2016-06-06 20:44:502233browse

我有个字符串,格式为$str = "中华人民abc共和\x01国",如何逐个汉字遍历该字符串呢?即:能够输出“中” “华” “人” “民” “a” ...

我现在使用的方法是:for($i =0 ; $i

请问应该如何提高效率?是否可以讲$str转换为数组?

回复内容:

我有个字符串,格式为$str = "中华人民abc共和\x01国",如何逐个汉字遍历该字符串呢?即:能够输出“中” “华” “人” “民” “a” ...

我现在使用的方法是:for($i =0 ; $i

请问应该如何提高效率?是否可以讲$str转换为数组?

<code class="lang-php">function str_split_unicode($str, $l = 0) {
if ($l > 0) {
$ret = array();
$len = mb_strlen($str, "UTF-8");
for ($i = 0; $i  中
    [1] => 华
    [2] => 人
    [3] => 民
    [4] => a
    [5] => b
    [6] => c
    [7] => 共
    [8] => 和
    [9] => \
    [10] => x
    [11] => 0
    [12] => 1
    [13] => 国
)
</code>

//UTF8算法,其它编码自行转换

<code class="lang-php"><br> $cind = 0;
 $arr_cont = array();
 for ($i = 0; $i  0) {
                if (ord(substr($tempaddtext, $cind, 1)) </code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:app上传图片Next article:thinkphp action与model问题,急