php对UTF8字体串进行单字分割返回数组

      在网上查了很多字符串分割方法,都无法正确对UTF8字符串进行正确分割返回单个字符的数组。经过对FTU8编码的分析写出了下面的方法对UTF8进行分割。本人测试可用。本方法只支持UTF8编码的,其它编码转自行转换成UT8再使用。

$tempaddtext="http://www.jishubu.net php对UTF8字体串进行单字分割返回数组";

//$tempaddtext=iconv("GBK","UTF-8",$tempaddtext); //字符编码转换,自行判定需要不需要
$cind = 0; $arr_cont = array(); for ($i = 0; $i < strlen($tempaddtext); $i++) { if (strlen(substr($tempaddtext, $cind, 1)) > 0) { if (ord(substr($tempaddtext, $cind, 1)) < 192) { if (substr($tempaddtext, $cind, 1) != " ") { array_push($arr_cont, substr($tempaddtext, $cind, 1)); } $cind++; } elseif(ord(substr($tempaddtext, $cind, 1)) < 224) { array_push($arr_cont, substr($tempaddtext, $cind, 2)); $cind+=2; } else { array_push($arr_cont, substr($tempaddtext, $cind, 3)); $cind+=3; } } } print_r($arr_cont);

  返回结果:

Array ( [0] => h [1] => t [2] => t [3] => p [4] => : [5] => / [6] => / [7] => w [8] => w [9] => w [10] => . [11] => j [12] => i [13] => s [14] => h [15] => u [16] => b [17] => u [18] => . [19] => n [20] => e [21] => t [22] => p [23] => h [24] => p [25] => 对 [26] => U [27] => T [28] => F [29] => 8 [30] => 字 [31] => 体 [32] => 串 [33] => 进 [34] => 行 [35] => 单 [36] => 字 [37] => 分 [38] => 割 [39] => 返 [40] => 回 [41] => 数 [42] => 组 )

  

posted @ 2014-07-08 08:32  manbuheiniu  阅读(905)  评论(0编辑  收藏  举报