Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split vs. explode in php

Tags:

php

What is the difference between explode and split in php?

like image 289
Tejas Patel Avatar asked Sep 11 '10 09:09

Tejas Patel


People also ask

What is difference between explode and split in PHP?

Both are used to split a string into an array, but the difference is that split() uses pattern for splitting whereas explode() uses string. explode() is faster than split() because it doesn't match string based on regular expression.

Why explode () is used?

explode() is a built in function in PHP used to split a string in different strings. The explode() function splits a string based on a string delimiter, i.e. it splits the string wherever the delimiter character occurs. This functions returns an array containing the strings formed by splitting the original string.

What is split in PHP?

Definition and Usage. The split() function will divide a string into various elements, the boundaries of each element based on the occurrence of pattern in string.


2 Answers

explode splits strings.

split (replaced with mb_split in newer versions of PHP) also does this, except it has support for splitting using regular expressions.

preg_split also does this and is 25-50% faster and has support for much more powerful Perl-compatible regular expressions.

like image 135
amphetamachine Avatar answered Oct 05 '22 05:10

amphetamachine


split uses regular expressions, while explode works with delimiter characters. Using split is discouraged, because it become deprecated in PHP 5.3.

like image 42
tamasd Avatar answered Oct 05 '22 07:10

tamasd