Skip to content

Instantly share code, notes, and snippets.

@frzsombor
Forked from ethaizone/mb_str_split.php
Created August 28, 2018 13:44
Show Gist options
  • Save frzsombor/847d90bb11fbbcf52ee5270cb20266a2 to your computer and use it in GitHub Desktop.
Save frzsombor/847d90bb11fbbcf52ee5270cb20266a2 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