Home >Backend Development >PHP Tutorial >Simple example of converting php string to array (supports Chinese)

Simple example of converting php string to array (supports Chinese)

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-25 08:55:471219browse
  1. /**
  2. * Convert string to array, support Chinese characters, limited to utf-8 format
  3. * Organized by: Script Academy bbs.it-home.org
  4. * @param $str
  5. * @return array
  6. */
  7. function StringToArray($str)
  8. {
  9. $result = array();
  10. $len = strlen($str);
  11. $i = 0;
  12. while($i < $len){
  13. $chr = ord($str[$i]);
  14. if($chr == 9 || $chr == 10 || (32 <= $chr && $chr <= 126)) {
  15. $result[] = substr($str,$i,1);
  16. $i +=1;
  17. }elseif(192 <= $chr && $chr <= 223){
  18. $result[] = substr($str,$i,2);
  19. $i +=2;
  20. }elseif(224 <= $chr && $chr <= 239){
  21. $result[] = substr($str,$i,3);
  22. $i +=3;
  23. }elseif(240 <= $chr && $chr <= 247){
  24. $result[] = substr($str,$i,4);
  25. $i +=4;
  26. }elseif(248 <= $chr && $chr <= 251){
  27. $result[] = substr($str,$i,5);
  28. $i +=5;
  29. }elseif(252 <= $chr && $chr <= 253){
  30. $result[] = substr($str,$i,6);
  31. $i +=6;
  32. }
  33. }
  34. return $result;
  35. }
复制代码


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