PHP按字符串长度分割成数组代码(中英文混杂字符进行分割)



   print_r($this->ch2arr('123肉rbv地 方'));
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 肉
    [4] => r
    [5] => b
    [6] => v
    [7] => 地
    [8] =>  
    [9] => 方
)

print_r($this->mbStrSplit('123肉rbv地 方',2));

Array
(
    [0] => 12
    [1] => 3肉
    [2] => rb
    [3] => v地
    [4] =>  方
)

//空格也算一个字符
//按1个字截取成数组
   function ch2arr($str)
    {
        $length = mb_strlen($str, 'utf-8');
        $array = [];
        for ($i=0; $i<$length; $i++)
            $array[] = mb_substr($str, $i, 1, 'utf-8');
        return $array;
    }

///自定义字数 截取成数组
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;
}

 

你可能感兴趣的:(php)