PHP str_split Function | Convert A String To An Array

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.

Syntax

str_split($string, $length = 1)

Parameters

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.

Return Value

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 length and each sub-string is returned as an individual array element.

PHP str_split Function
PHP str_split Function

Examples

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
(
    [0] => C
    [1] => o
    [2] => n
    [3] => v
    [4] => e
    [5] => r
    [6] => t
    [7] =>  
    [8] => s
    [9] => t
    [10] => r
    [11] => i
    [12] => n
    [13] => g
    [14] =>  
    [15] => t
    [16] => o
    [17] =>  
    [18] => a
    [19] => r
    [20] => r
    [21] => a
    [22] => 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
(
    [0] => Convert
    [1] =>  string
    [2] =>  to arr
    [3] => 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
(
    [0] => 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.

Conclusion

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.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *