/** 
     * 将字符串拆分为指定最大宽度的字符串数组。单字节字符宽度为1,多字节字符通常宽度为2
     * @param string $msg 要拆分的字符串
     * @param int $width 结果数组中每个元素的最大宽度。如10代表10个英文字母或者5个汉字
     * @param string $enc 字符串编码
     */
    function split_str2array_cn($msg,$width,$enc = 'utf-8'){
        $msg_width = mb_strwidth($msg,$enc);
        if($msg_width <= $width) return array($msg);
            
        $return = array();
        $msg_len = mb_strlen($msg,$enc);
        for($i = 0;$i<$msg_len;$i++){
            $temp_str .= mb_substr($msg,$i,1,$enc);
            if(mb_strwidth($temp_str,$enc) >= $width){ 
                array_push($return,$temp_str);
                $temp_str = '';
            }   
        }           
        if($temp_str != '') array_push($return,$temp_str);
        return $return;
    }               

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-02
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-22
  • 2022-12-23
  • 2021-11-12
  • 2021-12-23
相关资源
相似解决方案