The mb_str_split Function

The mb_str_split function splits a string in a multibyte encoding (for example, UTF-8) into an array of characters or parts of a specified length. The first parameter is the string to split, the second is the length of each part, and the third is the encoding.

Syntax

phpmb_str_split(string, [length], [encoding]);

Example

Let's split a string into individual characters:

php<?php
  $res = mb_str_split('абвгд');
  print_r($res);
?>

Code execution result:

php['а', 'б', 'в', 'г', 'д']

Example

Let's split a string into parts of 2 characters each:

php<?php
  $res = mb_str_split('12345', 2);
  print_r($res);
?>

Code execution result:

php['12', '34', '5']

Example

Let's split a string with explicit specification of UTF-8 encoding:

php<?php
  $res = mb_str_split('日本語', 1, 'UTF-8');
  print_r($res);
?>

Code execution result:

php['日', '本', '語']

See Also

  • the str_split function,
    which splits a string into an array
  • the explode function,
    which splits a string by a separator