Home >Backend Development >PHP Tutorial >PHP supports Chinese string splitting functions, _PHP tutorial

PHP supports Chinese string splitting functions, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:52:28818browse

php supports the Chinese string splitting function,

str_split does not support Chinese, use the mb_xx function to implement this

/**
 * Convert a string to an array
 * @param string $str
 * @param number $split_length
 * @return multitype:string
 */
function mb_str_split($str,$split_length=1,$charset="UTF-8"){
  if(func_num_args()==1){
    return preg_split('/(&#63;<!^)(&#63;!$)/u', $str);
  }
  if($split_length<1)return false;
  $len = mb_strlen($str, $charset);
  $arr = array();
  for($i=0;$i<$len;$i+=$split_length){
    $s = mb_substr($str, $i, $split_length, $charset);
    $arr[] = $s;
  }
  return $arr;
}

Method 2:

function mbStrSplit ($string, $len=1) {
  $start = 0;
  $strlen = mb_strlen($string);
  while ($strlen) {
    $array[] = mb_substr($string,$start,$len,"utf8");
    $string = mb_substr($string, $len, $strlen,"utf8");
    $strlen = mb_strlen($string);
  }
  return $array;
}

The above is the entire content of this article, I hope you all like it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1008003.htmlTechArticlephp supports Chinese string splitting function, str_split does not support Chinese, use mb_xx function to achieve /** * Convert a string to an array * @param string $str * @param number $split_leng...
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