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
Syntax
str_split(string $string, int $length = 1): array
Parameters
| Parameter | Description |
|---|---|
string |
The input string. |
length optional |
The size of each chunk in bytes. Default: 1 |
Return value
An array of strings.
Examples
<?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
)<?php
echo implode(" ", str_split("4111222233334444", 4)), "\n";
Output
4111 2222 3333 4444
Version history
In other languages
string.split()· JavaScript
Related
mb_str_split()· Split a multibyte string into an array of characters, or of chunks of n characters. The UTF-8-safe str_split().explode()· Split a string into an array by a separator, like turning "a,b,c" into ["a", "b", "c"]. A limit caps the pieces.- PHP Strings · tutorial