Difference Between Split and Explode in PHP

The split() and explode() are two main string function in php. If you are working in php, You must have used these php function in your application. You have also faced some question in interviews regarding difference between php split and explode string function.

In this post, I will let you know what are the differences between Split() and Explode() php function with example.

Simple Usage and example of split() PHP function

The split() function is use to splits the string into an array using a regular expression.The split method returns an array of string data. PHP split() function takes three arguments, first parameter is pattern regular expression which is case sensitive, second is input string and third one is for limit: If the limit is set, the returned array will contain a maximum of limit elements with the last element containing the whole rest of string.

Warning
This function was DEPRECATED in PHP 5.3.0, and REMOVED in PHP 7.0.0.Alternatives of split function in php7 –

You can read more from Implode And Explode In PHP 7 tutorial.

How to use PHP split Function

split(":", "this:is:a:string"); //returns an array that contains this, is, a, string.

Output :

Array(
[0] => this,
[1] => is,
[2] => a,
[3] => string
)

Simple Usage and Example of explode() PHP function

The php explode() function splits the string using delimiter and returns an array. PHP explode function takes three argument, first one takes delimiter as a argument, second one is target string and third one is limit : If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string.

Simple Example and Demo of PHP explode function

The below example is used to explode the string using delimiter, The delimiter is this string. Passing first one argument as a delimiter and second one is source string.

explode ("this", "this is a string"); //returns an array that contains array "is a string"

Output

array([0] => "is a string")

Leave a Reply

Your email address will not be published. Required fields are marked *