What is the difference between split() and explode()?
Asked Answered
D

6

37

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?

Defrock answered 4/9, 2010 at 5:6 Comment(2)
Split gives you string fragments, whereas explode gives you machine fragments. Eh, probably not ... :-)Dulcedulcea
@Peter Rowell: split gives you two strands of hair, whereas explode gives you shrapnel.Massarelli
M
60

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.

Massarelli answered 4/9, 2010 at 5:9 Comment(2)
So they messed up when they named it, apparently? Sounds like it should have been called ereg_split().Defrock
@hopeseekr: PHP always messes up naming of functions ;) And yeah, I guess you could say that!Massarelli
C
6

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).

Collator answered 4/9, 2010 at 5:8 Comment(0)
C
2

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.

E.g explode (" this", "this is a string"); will return “Is a string

E.g Split (" + ", "This+ is a string");

Clatter answered 4/9, 2010 at 6:40 Comment(0)
E
1

In split() you can use regular expressions to split a string. Whereas explode() splits a string with a string. preg_split is a much faster alternative, should you need regular expressions.

Earphone answered 4/9, 2010 at 5:9 Comment(0)
S
1

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.

Sharleensharlene answered 2/12, 2015 at 6:57 Comment(0)
P
0

The split() function splits the string into an array using a regular expression and returns an array.

The explode() function splits the string by string.

E.g:

<?php
    $split_array=split(':','I:P:S');
    $explode_array=explode('and','I and P and S');
    print_r($split_array);
    print_r($explode_array);
?>

The result will be

Array ( [0] => I [1] => P [2] => S ) 
Array ( [0] => I [1] => P [2] => S )

Note:The function split() was DEPRECATED in PHP 5.3.0, and REMOVED in PHP 7.0.0

Punkie answered 28/1, 2015 at 10:0 Comment(2)
Hi Johnny Come Lately!!Defrock
I am not sure I understand this answer. I don't understand what is being illustrated, particularly with this: $explode_array=split('and','I and P and S'); The line is also using the split() function...Fermium

© 2022 - 2024 — McMap. All rights reserved.