The mb_split Function

The mb_split function splits a string into parts by a delimiter, correctly working with multibyte encodings (such as UTF-8). The first parameter is the delimiter, the second is the string to be processed, and the third optional parameter limits the number of elements in the resulting array.

Syntax

phpmb_split(string $pattern, string $string, int $limit = -1): array

Example

Let's split a string by commas:

php<?php
  $res = mb_split(',', 'a,b,c,d,e');
  print_r($res);
?>

Code execution result:

php['a', 'b', 'c', 'd', 'e']

Example

Let's split a string with Cyrillic characters by spaces:

php<?php
  $res = mb_split('\s', 'привет мир тест');
  print_r($res);
?>

Code execution result:

php['привет', 'мир', 'тест']

Example

Let's limit the number of elements in the result:

php<?php
  $res = mb_split(',', '1,2,3,4,5', 3);
  print_r($res);
?>

Code execution result:

php['1', '2', '3,4,5']

See Also

  • the preg_split function,
    which splits a string by a regular expression
  • the explode function,
    which splits a string by a simple delimiter