mb_str_split
Splits a string into characters for any alphabet.
Syntax
mb_str_split(string $string, int $length = 1, ?string $encoding = null): arrayParameters
| Parameter | Type | Description |
|---|---|---|
| $string | string | The source string. |
| $length | int | Optional. How many characters go into each piece. |
| $encoding | ?string | Optional. The encoding; UTF-8 by default. |
Return value
arrayAn array of characters or groups of characters.
Examples
Cyrillic letters:
<?php
echo implode(' ', mb_str_split('кот'));Output:
к о тReversing a string in any language:
<?php
echo implode('', array_reverse(mb_str_split('привет')));Output:
тевирпNotes
The function appeared in PHP 7.4. Plain strrev() produces garbage for non-ASCII text — reverse with mb_str_split() instead.
See also
Related recipes
In the tutorial
- Strings — Working with strings in PHP — quotes, concatenation, length, searching, replacing and multibyte functions.