Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between split() and explode()?

Tags:

php

posix-ere

The PHP manual for split() says

This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged...Use explode() instead.

But I can't find a difference between split() and explode(). join() hasn't been deprecated, so what gives?

like image 719
Theodore R. Smith Avatar asked Sep 04 '10 05:09

Theodore R. Smith


People also ask

What does the explode () function do?

The explode function is utilized to "Split a string into pieces of elements to form an array". The explode function in PHP enables us to break a string into smaller content with a break. This break is known as the delimiter.

What does the explode () function return?

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.

Is split () deprecated?

split() is deprecated as of PHP 5.3. 0. preg_split() is the suggested alternative to this function. If you don't require the power of regular expressions, it is faster to use explode(), which doesn't incur the overhead of the regular expression engine.


2 Answers

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.

like image 185
BoltClock Avatar answered Sep 23 '22 11:09

BoltClock


split uses regex, while explode uses a fixed string. If you do need regex, use preg_split, which uses PCRE (the regex package now preferred across the PHP standard library).

like image 37
Matthew Flaschen Avatar answered Sep 23 '22 11:09

Matthew Flaschen