Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

Difference Between Split and Explode in PHP

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

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() and explode() are two main string function in php, if you are working in php, you would have used this function on your application. You have also faced some question in interview regarding difference between php split and explode string function. In this post, I will let you know what are difference between Split and Explode with example.


Simple Usage and example of split() PHP function

The split() function splits the string into an array using a regular expression and returns an array. PHP split function takes three arguments, first one takes pattern regular expression which is case sensitive, second is input string and thirst 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.

How to use PHP split Function

<?php split(“:”, “this:is:a:string”); //returns an array that contains this, is, a, string. ?>

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

Warning
This function was DEPRECATED in PHP 5.3.0, and REMOVED in PHP 7.0.
Alternatives to this function include: preg_split(), explode(), str_split()

Simple Usage and Example of explode() PHP function

Explode is much faster than split , because split uses regular expression to split string. explode splits a string by string.

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.

<?php explode (“this”, “this is a string”); //returns an array that contains array “is a string” ?>

Output
array(
[0] => “is a string”
)

If you have any query or suggestions, feel free to ask me via the comment section below.

must watch some other topics :

cake PHP training institute
cake PHP training course
cake php training online

Post a Comment

0 Comments