Split utf8 string into array of chars

后端 未结 6 1934
别那么骄傲
别那么骄傲 2021-01-14 05:26

I\'m trying to split a utf8 encoded string into an array of chars. The function that I now use used to work, but for some reason it doesn\'t work anymore. W

6条回答
  •  一个人的身影
    2021-01-14 06:04

    If you not sure about availability of mb_string function library, then use:

    Version 1:

    function utf8_str_split($str='',$len=1){
        preg_match_all("/./u", $str, $arr);
        $arr = array_chunk($arr[0], $len);
        $arr = array_map('implode', $arr);
        return $arr;
    }
    

    Version 2:

    function utf8_str_split($str='',$len=1){
        return preg_split('/(?<=\G.{'.$len.'})/u', $str,-1,PREG_SPLIT_NO_EMPTY);
    }
    

    Both functions tested in PHP5

提交回复
热议问题