What is the difference between explode and split?

Last updated 5 years ago | 1507 views 75     5

Tags:- PHP

The explode() and split() both function is use to split a string into an array. But the difference is explode() function splits a string into array by string where as Split function splits string into array by regular expression.

 

Let's see an example from both

explode()

<?php

$explode = explode("and", "Roshan and Shyam and Rahul");

print_r($explode);

?>

//Output

Array
			(
				[0] => Roshan
				[1] => Shyam
				[2] => Rahul
			)

 

split()

<?php

$split = split(" :", "Roshan : Shyam : Rahul");

print_r($split);

?>


//Output

Array
        (
            [0] => Roshan
            [1] => Shyam
            [2] => Rahul
        )

 

Above both function is returning the same array.