Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

str_split

Splits a string into pieces of a given length.

Syntax

str_split(string $string, int $length = 1): array

Parameters

ParameterTypeDescription
$stringstringThe source string.
$lengthintOptional. The length of each piece; 1 by default.

Return value

arrayAn array of the pieces.

Examples

One character at a time:

<?php
 
echo implode(' ', str_split('abc'));

Output:

a b c

Groups of four digits:

<?php
 
echo implode(' ', str_split('1234567812345678', 4));

Output:

1234 5678 1234 5678

Notes

The function counts bytes. Use mb_str_split() for non-ASCII text and emoji.

See also

  • explode — Splits a string into an array by a separator.
  • mb_str_split — Splits a string into characters for any alphabet.

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.