PHP 8 reference · String Functions

PHP str_split() Function

Split a string into an array of chunks of a given length, one byte each by default. Use mb_str_split() for UTF-8.

PHP 8 Since 5.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

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

Parameters

ParameterDescription
string The input string.
length optional The size of each chunk in bytes. Default: 1

Return value

An array of strings.

Examples

Characters and chunksTry it yourself
<?php
print_r(str_split("PHP"));
print_r(str_split("ABCDEFG", 3));

Output

Array
(
    [0] => P
    [1] => H
    [2] => P
)
Array
(
    [0] => ABC
    [1] => DEF
    [2] => G
)
Group digitsTry it yourself
<?php
echo implode(" ", str_split("4111222233334444", 4)), "\n";

Output

4111 2222 3333 4444

Version history

VersionChange
PHP 5.0Added
PHP 8.2An empty string returns an empty array instead of [""].

In other languages

Was this page helpful?