-4

Can this work for splitting a multi-byte string every ten characters?

$string = 'Star Wars Episode Seven Sucked';    
mb_split('.', $string, 10);

The PHP manual says that str_split() works on bytes, not characters in multi-byte strings. That means mb_split() would seem to be a natural "overloaded" substitution, but the two functions (str_split() and mb_split()) have different function signatures and are not "overload buddies," so to speak. Then, I had a thought, what about this?

mb_internal_encoding("UTF-8");
$string = 'Star Wars Episode Seven Sucked';  
$tokens = [];

for($i = 0, $length = mb_strlen($string); $i < $length; $i += 10)
{
    $tokens[] = mb_substr($string, $i, 10, 'UTF-8');
}

print_r($tokens);
Melinda
  • 76
  • 8

1 Answers1

-1
function mb_utf8_split($string, $interval)
{
     $tokens = [];
     mb_internal_encoding('UTF-8');

     $stringEncoding = mb_detect_encoding($string, 'UTF-8, ISO-8859-1', true);

     if(!$stringEncoding)
     {
          throw new RuntimeException("Unable to identify character encoding.");
     }

     if($stringEncoding !== 'UTF-8')
     {
          $string = mb_convert_encoding($string, 'UTF-8', $stringEncoding);
     }

     for($i = 0, $length = mb_strlen($string); $i < $length; $i += $interval)
     {
         $tokens[] = mb_substr($string, $i, $interval, 'UTF-8');
     }

     return $tokens;
}
Melinda
  • 76
  • 8