The str_split() function is used to split a string into an array. This function was introduced in PHP5. The function splits the string into an array of characters, each containing length characters; if the length is not specified, it defaults to 1.
Syntax:
str_split(string $string, int $length = 1): array
Parameters
Parameter | Description |
---|---|
string | The input string. |
length | Maximum length of the chunk. |
Return Values
false is returned if length is less than 1. If the length length exceeds the length of string, the entire string is returned as the first (and only) array element.
Examples
Example 1:
<?php $text = “How are you”; $split1 = str_split($text); $split2 = str_split($text, 3); print_r($split1); print_r($split2); ?>
The output of the above program is as follows:
Array ( [0] => H [1] => o [2] => w [3] => [4] => a [5] => r [6] => e [7] => [8] => y [9] => o [10] => u ) Array ( [0] => How [1] => ar [2] => e y [3] => ou )