Skip to content

Instantly share code, notes, and snippets.

@ethaizone
Created January 23, 2016 16:52
Show Gist options
  • Save ethaizone/d452528f10d59dd48735 to your computer and use it in GitHub Desktop.
Save ethaizone/d452528f10d59dd48735 to your computer and use it in GitHub Desktop.
mb_str_split function. multibyte version of str_split.
<?php
if (!function_exists('mb_str_split')) {
/**
* Convert a multibyte string to an array
*
* @param string $string The input string.
* @param integer $split_length Maximum length of the chunk.
* @param string $encoding The encoding parameter is the character encoding.
* @return array
*/
function mb_str_split($string, $split_length = 1, $encoding = null)
{
if (is_null($encoding)) {
$encoding = mb_internal_encoding();
}
if ($split_length < 1) {
return false;
}
$return_value = array();
$string_length = mb_strlen($string, $encoding);
for ($i = 0; $i < $string_length; $i += $split_length)
{
$return_value[] = mb_substr($string, $i, $split_length, $encoding);
}
return $return_value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment