Detailed explanation of function examples in php that support Chinese string splitting

怪我咯
Release: 2023-03-13 07:06:01
Original
1001 people have browsed it

This article shares with you two PHP methods for using the mb_xxx method to achieve Chinese character segmentation. The basic ideas are similar. Friends in need can refer to it.

str_split does not support Chinese, use mb_xx function to implement

/**
 * 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(&#39;/(?<!^)(?!$)/u&#39;, $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;
}
Copy after login

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;
}
Copy after login

The above is the detailed content of Detailed explanation of function examples in php that support Chinese string splitting. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!