str_split
Splits a string into pieces of a given length.
Syntax
str_split(string $string, int $length = 1): arrayParameters
| Parameter | Type | Description |
|---|---|---|
| $string | string | The source string. |
| $length | int | Optional. 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 cGroups of four digits:
<?php
echo implode(' ', str_split('1234567812345678', 4));Output:
1234 5678 1234 5678Notes
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.