PHP str_split is an inbuilt function in PHP. It converts a string into an array. Also, if you specify the length, it converts the string into sub-strings and returns them as array elements. In this article, we will discuss the PHP str_split Function. Also, we will discuss a few examples of using it.
str_split($string, $length = 1)
The PHP str_split Function expects two parameters. However, one parameter is mandatory and the other is optional. The description of the parameters is as follows:
- $string: The original string to split and convert into an array. It’s a mandatory parameter of type “string”.
- $length: The length by which we split the string or length of each array element. It is an optional parameter of type “int”. The default value for $lenght parameter is 1.
The PHP str_split function returns an array. As stated above, if you don’t specify any length then by default the function treats it as 1. Each character of the string splits and returns as each individual array element. However, if you specify the length then the string is split into sub-strings of specified
Let us quickly see some examples to demonstrate the use of the PHP str_split function.
Example 1: Splitting Each Character
Here, we split the string by 1 (as we don’t pass value to length parameter).
$string = "Convert string to array"; $output = str_split($string); print_r($output);
OUTPUT: Array (  => C  => o  => n  => v  => e  => r  => t  =>  => s  => t  => r  => i  => n  => g  =>  => t  => o  =>  => a  => r  => r  => a  => y )
We can clearly see that each character of the string converts into each array element.
Example 2: Specifying the length
In this example we give 7 as the value to $length parameter and analyse the output.
$string = "Convert string to array"; $output = str_split($string,7); print_r($output);
OUTPUT: Array (  => Convert  => string  => to arr  => ay )
From the output above, we see that string is split into sub-strings of 7 characters each. Each sub-string converts into each array element.
Example 3: Length Greater Than String Length
Let us see what happens if we give the value of $lenght to be greater than the string itself.
$string = "Convert string to array"; $output = str_split($string,100); print_r($output);
OUTPUT: Array (  => Convert string to array )
The string converts into a single array element as whole if we pass value of the $lenght parameter greater than the string itself.
However, If we pass the value of $length parameter as negative then it throws below error:
str_split(): The length of each segment must be greater than zero.
In conclusion, we discussed the PHP str_split Function. You can learn more about it from PHP’s official documentation. Also, do learn about PHP’s array functions and other string functions from Concatly.
Riya is currently working with adda52.com as a Senior Software Engineer. She holds a degree in Computer Science Engineering from MDU. Her interests include adventurous sports and traveling. She loves to code for Web Services, Scripting Language, and SQL.