PHP str_split() Function
Definition
The str_split() function splits a string into an array.
Syntax
PHP str_split() Function has the following syntax.
str_split(string,length)
Parameter
Parameter | Is Required | Description |
---|---|---|
string | Required. | String to split |
length | Optional. | Length of each array element. Default is 1 |
Return
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.
Example 1
Split the string "Hello" into an array:
<?php
print_r(str_split("Hello"));
?>
The code above generates the following result.
Example 2
Using the length parameter:
<?php
print_r(str_split("Hello from java2s.com",3));
?>
The code above generates the following result.