Home > Backend Development > PHP Tutorial > Multiple methods for splitting Chinese strings in php

Multiple methods for splitting Chinese strings in php

WBOY
Release: 2016-07-25 08:51:38
Original
1172 people have browsed it
  1. $str="Scripting School: http://bbs.it-home.org";
  2. function mbstringtoarray($str,$charset) {
  3. $strlen=mb_strlen($str) ;
  4. while($strlen){
  5. $array[]=mb_substr($str,0,1,$charset);
  6. $str=mb_substr($str,1,$strlen,$charset);
  7. $strlen=mb_strlen ($str);
  8. }
  9. return $array;
  10. }
  11. $arr=mbstringtoarray($str,"gb2312");
  12. ?>
Copy code

Note: 1. The $charset variable is the web page encoding, such as "gb2312" or "utf-8"; 2. The first method requires that the server must enable the mbstring.dll extension, otherwise the code will execute incorrectly, so for those who use virtual hosts, you can consider using the second method.

Method 2:

  1. function str_to_arr($str){
  2. $l=strlen($str);
  3. for($i=0;$i<$l;$i++){
  4. $arr[ ]=ord($str[$i])>127?$str[$i].$str[++$i]:$str[$i];
  5. }
  6. return $arr;
  7. }
  8. $arr =str_to_arr($str);
  9. ?>
Copy code


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template