W3docs

str_split()

Our article is about the PHP function str_split(), which is used to split a string into an array. In this article, we will discuss the syntax and usage of

The PHP str_split() function breaks a string into an array of smaller pieces. By default each piece is a single character, but you can ask for fixed-length chunks instead. It is the go-to function when you need to iterate over a string character by character or process it in equal-sized blocks.

This page covers the syntax, the chunk-length parameter, the value it returns, and the multibyte gotcha that trips most developers up.

Syntax

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

The function accepts two parameters:

  • $string — the string to split. Required.
  • $length — the length of each chunk, in bytes. Optional; defaults to 1. Must be 1 or greater (a value less than 1 triggers a ValueError in PHP 8.0+).

It returns an array of string chunks. If $string is empty, the result is an empty array ([]) in PHP 8.2 and later. The last chunk may be shorter than $length when the string length is not an exact multiple of it.

Splitting into single characters

With no second argument, str_split() returns one array element per character. This is handy for looping over every character or counting characters by category.

php— editable, runs on the server

The output is an array where each character has its own index:

Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] => ,
    [6] =>  
    [7] => W
    [8] => o
    [9] => r
    [10] => l
    [11] => d
    [12] => !
)

Splitting into fixed-length chunks

Pass a $length to group the characters into chunks of that size. This is useful for formatting strings such as breaking a card number into groups, or processing binary data in fixed blocks.

<?php
$string = "Hello, World!";
$chunks = str_split($string, 3);
print_r($chunks);
?>

The string has 13 characters, so it splits into four chunks of 3 plus a final chunk of 1:

Array
(
    [0] => Hel
    [1] => lo,
    [2] =>  Wo
    [3] => rld
    [4] => !
)

Watch out for multibyte strings

str_split() works on bytes, not characters. For ASCII text one byte equals one character, so the default behaviour looks like character splitting. But UTF-8 characters such as é or emoji occupy multiple bytes, and str_split() will tear them apart, producing invalid fragments:

<?php
print_r(str_split("héllo"));
?>
Array
(
    [0] => h
    [1] => Ã
    [2] => ©
    [3] => l
    [4] => l
    [5] => o
)

The é came out as two broken bytes. When you need to split a UTF-8 string by real characters, use mb_str_split() instead — it is multibyte-aware and keeps each character intact.

When to use str_split() vs. explode()

Reach for str_split() when you want to break a string at a fixed position or length — every single character, or every N bytes. Use explode() when you want to split on a delimiter such as a comma or a space. To rebuild a string from an array, see implode(), and to split on a regular expression, see preg_split().

Practice

Practice
What does the PHP 'str_split()' function do?
What does the PHP 'str_split()' function do?
Was this page helpful?