Backend Development 2 min read

PHP str_split() Function: Description, Parameters, Return Values, and Examples

This article explains PHP's str_split() function, detailing its purpose of converting a string to an array, describing its parameters, return behavior, and providing example code demonstrating default and custom split lengths along with the resulting output.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP str_split() Function: Description, Parameters, Return Values, and Examples

The str_split() function in PHP converts a string into an array of smaller strings.

Signature: array str_split(string $string [, int $split_length = 1])

Parameters:

string : The input string to be split.

split_length : The length of each chunk. If omitted, each character becomes a separate element. If set to a value less than 1, the function returns FALSE . If the value exceeds the length of the string, the whole string is returned as a single element.

Return value: An array where each element is a substring of length split_length (or a single character when the parameter is omitted).

Example 1 – Default split length (1):

<?php
$str = "Hello Friend";
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);
?>

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
)
BackendPHParraystring-manipulationstr_split
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.