PHP - str_split() Function
The PHP str_split() function is used to split the given string into an array. This function has an optional parameter which is used to specify the number of characters for each split with default value 1. Please note that it does not change the original string.
Syntax
str_split(string, length)
Parameters
string |
Required. Specify the string to split |
length |
Optional. Specify length of each element of the array. Default is 1. |
Return Value
Returns the array containing split string as elements of the array.
Example:
In the below example, str_split() function to used to split string MyString and return an array containing split string as elements of the string.
<?php $MyString = "Hello World!"; $NewString = str_split($MyString,3); print_r($NewString); ?>
The output of the above code will be:
Array ( [0] => Hel [1] => lo [2] => Wor [3] => ld! )
❮ PHP String functions