Skip to content

Instantly share code, notes, and snippets.

@caingougou
Created November 5, 2010 05:47
Show Gist options
  • Save caingougou/663702 to your computer and use it in GitHub Desktop.
Save caingougou/663702 to your computer and use it in GitHub Desktop.
str_split function for multibytes
<?php
function mb_str_split($str, $split_length) {
$chars = array();
$len = mb_strlen($str);
for ($i = 0; $i < $len; $i+=$split_length ) {
$chars[] = mb_substr($str, $i, $split_length); // only one char to go to the array
}
return $chars;
}
@manzoorwanijk
Copy link

I passed $split_length as 6 for a string containing Arabic characters. It did't return the actual number of characters, rather it returned only 5 characters for $char[0] => sdبsd. Then I added 'UTF-8' parameter to mb_strlen() & mb_substr() and it worked like charm
Thank you :)

function mb_str_split($str, $split_length) {
    $chars = array();
    $len = mb_strlen($str, 'UTF-8'); 
    for ($i = 0; $i < $len; $i+=$split_length ) {
        $chars[] = mb_substr($str, $i, $split_length, 'UTF-8');
    }
    return $chars;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment