What's the difference between explode() and str_split() functions in php ?
First, Let's agree that explode() and str_split() have the same work, they change the string into an array. but whereas the explode()
function converts whole words that make up the string into separate arrays, the str_split()
function converts every character in the string into an array item unless told otherwise.
Example:
print_r(str_split("My name"));
//output :
Array ( [0] => M [1] => y [2] => [3] => n [4] => a [5] => m [6] => e )
print_r (explode(" ","My name"));
//output :
Array ( [0] => My [1] => name )
/* Notice
To transform a string into an array using explode function,
we must have at least a space or any characters to separate it.
first argument can't be an empty string like :
-> explode("","hello guys)
Be careful :
"" : empty string
" ": space
To convert a string into an array ,we need a seperator in the string.
*/