What is the best way to split a string into an array of Unicode characters in PHP?

前端 未结 7 2424
野的像风
野的像风 2020-12-05 15:23

In PHP, what is the best way to split a string into an array of Unicode characters? If the input is not necessarily UTF-8?

I want to know whether the set of Unicode

7条回答
  •  無奈伤痛
    2020-12-05 15:46

    the best way for split with length: I just changed laravel str_limit() function:

        public static function split_text($text, $limit = 100, $end = '')
    {
        $width=mb_strwidth($text, 'UTF-8');
        if ($width <= $limit) {
            return $text;
        }
        $res=[];
        for($i=0;$i<=$width;$i=$i+$limit){
            $res[]=rtrim(mb_strimwidth($text, $i, $limit, '', 'UTF-8')).$end;
        }
         return $res;
    }
    

提交回复
热议问题