For mb_...
functions, you must specify the encoding encoding.
In your code example, these are especially the following two lines:
$strLen = mb_strlen($str, 'UTF-8'); $arr[] = mb_substr($str, $i, $len, 'UTF-8');
Full picture:
function utf8Split($str, $len = 1) { $arr = array(); $strLen = mb_strlen($str, 'UTF-8'); for ($i = 0; $i < $strLen; $i++) { $arr[] = mb_substr($str, $i, $len, 'UTF-8'); } return $arr; }
Because you are using UTF-8 here. However, if the input is incorrectly encoded, it will not work “anymore” - just because it is not intended for something else.
You can take turns processing UTF-8 encoded strings with PCRE regular expressions, for example, this will return what you are looking for in less code:
$str = 'Zelf heb ik maar één vraag: wie ben jij?'; $chars = preg_split('/(?!^)(?=.)/u', $str);
Next to preg_split
also mb_split
.
hakre source share