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
mb_split(string $pattern, string $string, int $limit = -1): array
Example
Let's split a string by commas:
<?php
$res = mb_split(',', 'a,b,c,d,e');
print_r($res);
?>
Code execution result:
['a', 'b', 'c', 'd', 'e']
Example
Let's split a string with Cyrillic characters by spaces:
<?php
$res = mb_split('\s', 'привет мир тест');
print_r($res);
?>
Code execution result:
['привет', 'мир', 'тест']
Example
Let's limit the number of elements in the result:
<?php
$res = mb_split(',', '1,2,3,4,5', 3);
print_r($res);
?>
Code execution result:
['1', '2', '3,4,5']
See Also
-
the
preg_splitfunction,
which splits a string by a regular expression -
the
explodefunction,
which splits a string by a simple delimiter