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

前端 未结 7 2501
野的像风
野的像风 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:45

    function str_split_unicode($str, $l = 0) {
        if ($l > 0) {
            $ret = array();
            $len = mb_strlen($str, "UTF-8");
            for ($i = 0; $i < $len; $i += $l) {
                $ret[] = mb_substr($str, $i, $l, "UTF-8");
            }
            return $ret;
        }
        return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);
    }
    var_dump(str_split_unicode("لأآأئؤة"));
    

    output:

    array (size=7)
      0 => string 'ل' (length=2)
      1 => string 'أ' (length=2)
      2 => string 'آ' (length=2)
      3 => string 'أ' (length=2)
      4 => string 'ئ' (length=2)
      5 => string 'ؤ' (length=2)
      6 => string 'ة' (length=2)
    

    for more information : http://php.net/manual/en/function.str-split.php

提交回复
热议问题