PHP str_split() Function – Convert a String to an Array
The article explains PHP's str_split() function, which converts a string into an array of characters or fixed‑length chunks, details its parameters and return behavior, and provides example code with output, including demonstrating how split_length affects the result.
The PHP str_split() function converts a string into an array of substrings.
It accepts two parameters: string – the input string, and optional split_length – the length of each segment (default 1). If split_length is less than 1, the function returns FALSE ; if it exceeds the string length, the entire string is returned as a single array element.
The function returns an array where each element contains a chunk of the original string, either a single character or a block of the specified length.
Example usage:
Output:
Array
(
[0] => H
[1] => e
[2] => l
[3] => l
[4] => o
[5] =>
[6] => F
[7] => r
[8] => i
[9] => e
[10] => n
[11] => d
)
Array
(
[0] => Hel
[1] => lo
[2] => Fri
[3] => end
)Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.