Difference between Split and Explode

split() and explode() functions are used to convert from string to array.

split() function

this function is removed from PHP7.

str_split() function

this function is used to convert from string to array.

Syntax:-


str_split ( string $string , int $length = 1 )

Note:- where int length is optional. If the optional length parameter is specified, the returned array will be broken down into chunks with each being length in length, otherwise, each chunk will be one character in length.

Example:- Suppose, you have a string and you want to split the string through str_split() function.


<?php 
$string="i am John";
$str_arr=str_split($string);
print_r($str_arr);
?>
Output:- Array ( [0] => i [1] => [2] => a [3] => m [4] => [5] => J [6] => o [7] => h [8] => n )

Try it Yourself


<?php 
$string="i am John";
$str_arr=str_split($string,2);
print_r($str_arr);
?>
Output:- Array ( [0] => i [1] => am [2] => J [3] => oh [4] => n )

Try it Yourself

explode() function

this function is used to convert from string to array using another string.

Example:- Suppose, you have a string and you split the string through “and” string.


<?php 
$string="John and Tony and Anna";
$str_arr=explode("and", $string);
print_r($str_arr);
?>
Output:- Array ( [0] => John [1] => Tony [2] => Anna )

Try it Yourself