HomeGeneral

PHP: What is the difference between split and explode php?

PHP: What is the difference between split and explode php?
Like Tweet Pin it Share Share Email

Difference between split and explode php

Do you know what is the main difference between split() and explode()?

No worries 🙂 , the main difference in split() and explode is the way, it uses to splitting a large string.

 

Both the functions are used to Split a string. However, Split is used to split a string using a regular expression. On the other hand, Explode is used to split a string using another string.

 

The split() function splits the string into an array using a regular expression and returns an array.
Ex: split (:India:Nepal:Srilanka); returns an array that contains India, Nepal, Srilanka.

 

The explode () function splits the string by string.
Ex: explode (and India and Nepal and Srilanka); returns an array that contains India, Nepal, Srilanka.

 

Have a look by below example.

split (":", "i:am:reading:scriptarticle.com");

returns an array that contains i,am,reading,scriptarticle.com.

 

 

The explode () function splits the string by string.

explode ("I", "I am reading scriptarticle.com");

returns an array that contains

array (
0 => ”,
1 => ‘ am reading scriptarticle.com’,
)

Note: split () function has been DEPRECATED as of PHP 5.3.0.

 

It’s been deprecated because

 

  • explode() is substantially faster because it doesn’t split based on a regular expression, so the string doesn’t have to be analyzed by the regex parser
  • preg_split() is faster and uses PCRE regular expressions for regex splits

 

join() and implode() are aliases of each other and therefore don’t have any differences.

 

Hope it helps!!