What’s the difference between Split and Explode?

Explode: explode is used to Split a string by string. It returns result set in an array format.

i.e. $string = "My Name is Praful Kumar";

$Array = explode(" ", $string);

print_r($Array);

Output:

Array (
    [0] => My 
    [1] => Name 
    [2] => is 
    [3] => Praful 
    [4] => Kumar
)
  1. Explode is used to split a string using another string. E.g explode("this","this is a string"); will return “Is a string”
  2. The explode() function splits the string by string. Ex: explode(and Praful and Kumar and mumbaikar); returns an array that contains Praful, Kumar, mumbaikar.
  3. Both the functions are used to split a string.

Split: split is used to Split string into array by regular expression. It also returns an array.

$string = "My Name is Praful Kumar";

$Array = split(" ", $string);

print_r($Array);

Output:

Array (
    [0] => My 
    [1] => Name 
    [2] => is 
    [3] => Praful 
    [4] => Kumar 
)
  1. Split is used to split a string using a regular expression. Eg. Split (" + ", "This+ is a string")
  2. The split() function splits the string into an array using a regular expression and returns an array. Ex: split(:Praful:Kumar:mumbaikar); returns an array that contains Praful, Kumar, mumbaikar.
  3. Both the functions are used to split a string.
Posted in PHP | Tagged , , , , , | Comments Off

Comments are closed.