首页 > 语言 > PHP > 正文

php中UTF8字符串进行单字分割返回数组(包含中文)

2024-09-04 11:46:35
字体:大 中 小
来源:转载
供稿:网友

下面来给大家介绍一个php中UTF8字符串进行单字分割返回数组(包含中文),这个例子非常的不错,测试了很多篇都非常的完美的解决字符串是uft8并且有中文时不出乱码哦,代码如下:

  1. $tempaddtext="php对UTF8字体串进行单字分割返回数组"; 
  2.    
  3. $cind = 0; 
  4. $arr_cont = array(); 
  5. for ($i = 0; $i < strlen($tempaddtext); $i++) { 
  6. if (strlen(substr($tempaddtext, $cind, 1)) > 0) { 
  7.  if (ord(substr($tempaddtext, $cind, 1)) < 192) { 
  8. if (substr($tempaddtext, $cind, 1) != " ") { 
  9.  array_push($arr_cont, substr($tempaddtext, $cind, 1)); 
  10. }  //开源软件:Vevb.com 
  11. $cind++; 
  12.  } elseif(ord(substr($tempaddtext, $cind, 1)) < 224) { 
  13. array_push($arr_cont, substr($tempaddtext, $cind, 2)); 
  14. $cind+=2; 
  15.  } else { 
  16. array_push($arr_cont, substr($tempaddtext, $cind, 3)); 
  17. $cind+=3; 
  18.  } 
  19. } 
  20.  } 
  21.    
  22. print_r($arr_cont); 
  23. 返回结果 
  24. Array 
  25. ( 
  26.     [0] => p 
  27.     [1] => h 
  28.     [2] => p 
  29.     [3] => 对 
  30.     [4] => U 
  31.     [5] => T 
  32.     [6] => F 
  33.     [7] => 8 
  34.     [8] => 字 
  35.     [9] => 体 
  36.     [10] => 串 
  37.     [11] => 进 
  38.     [12] => 行 
  39.     [13] => 单 
  40.     [14] => 字 
  41.     [15] => 分 
  42.     [16] => 割 
  43.     [17] => 返 
  44.     [18] => 回 
  45.     [19] => 数 
  46.     [20] => 组 
  47. )

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表