PHP: function mb_str_split


mbstring provides multibyte specific string functions that help you deal with multibyte encodings in PHP. In addition to that, mbstring handles character encoding conversion between the possible encoding pairs. mbstring is designed to handle Unicode-based encodings such as UTF-8 and UCS-2 and many single-byte encodings for convenience.

Method 1: base on preg_split function

<?php

function mb_str_split($string)
{
    return preg_split('/(?<!^)(?!$)/u', $string);
}
?>

Usage:

<?php
$string = 'Я люблю тебя';
$chars = mb_str_split($string);

print_r($chars);
?>

result:

Array
(
[0] => Я
[1] =>
[2] => л
[3] => ю
[4] => б
[5] => л
[6] => ю
[7] =>
[8] => т
[9] => е
[10] => б
[11] => я
)

Method 2: base on mb_substr function (PHP4 >= 4.0.6)

<?php

mb_internal_encoding('UTF-8');

function mb_str_split($str, $length = 1)
{
    if ($length < 1)
        return false;

    $result = array();

    for ($i = 0; $i < mb_strlen($str); $i += $length) {
        $result[] = mb_substr($str, $i, $length);
    }

    return $result;
}

?> 

Leave a Reply