Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

mb_str_split

Splits a string into characters for any alphabet.

Syntax

mb_str_split(string $string, int $length = 1, ?string $encoding = null): array

Parameters

ParameterTypeDescription
$stringstringThe source string.
$lengthintOptional. How many characters go into each piece.
$encoding?stringOptional. 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

  • str_split — Splits a string into pieces of a given length.
  • mb_strlen — Returns the number of characters in a string.

Related recipes

In the tutorial

  • Strings — Working with strings in PHP — quotes, concatenation, length, searching, replacing and multibyte functions.

Thanks for reading!

Donate to the site — we work hard for you.