The str_split() function splits a string into an array.
PHP str_split() Function has the following syntax.
str_split(string,length)
Parameter | Is Required | Description |
---|---|---|
string | Required. | String to split |
length | Optional. | Length of each array element. Default is 1 |
If length is less than 1, the str_split() function will return FALSE.
If length is larger than the length of string, the entire string is returned as the only element in the array.
Split the string "Hello" into an array:
<?php
print_r(str_split("Hello"));
?>
The code above generates the following result.
Using the length parameter:
<?php
print_r(str_split("Hello from java2s.com",3));
?>
The code above generates the following result.