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;
}

?> 

Recent search terms:

  • fence9bb
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1831 FI3q_LkkUqAi-tXLPyhAARWSFsSMGZFH2qv8lGXpgI2Peh69k_ytkj9A2fXf9qZE 556175c08621af45c75384c8eb1070f6008310ef&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • jungletrj
  • RVFI

Leave a Reply