The difference between PHP functions explode and split

Source: Internet
Author: User
Tags explode regular expression stack trace expression engine

Objective

This is done because the two functions are similar in that they are converting strings into groups.

Explode

As can be seen from the example below, the resulting array is in a corresponding order.

The code is as follows Copy Code

$pizza = "Piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = Explode ("", $pizza);
echo $pieces [0]; Piece1
echo $pieces [1]; Piece2

Example 2
$data = "Foo:*:1023:1000::/home/foo:/bin/sh";
List ($user, $pass, $uid, $gid, $gecos, $home, $shell) = Explode (":", $data);
Echo $user; Foo
Echo $pass; // *

Note that if the first argument is an empty string, a warning is generated.

The code is as follows Copy Code

Var_dump (Explode (', ' asdasd '));

Warning:explode (): Empty delimiter In/tmp/e80c9663-e392-4f81-8347-35726052678f/code on line 3
BOOL (FALSE)
Split

(PHP 4, PHP 5)

Split-the string into an array with a regular expression

Note that PHP 7 is not on the top, which means that the split function does not support PHP 7.

The code is as follows Copy Code

$date = "04/30/1973";
List ($month, $day, $year) = Split (' [/.-] ', $date);
echo "Month: $month; Day: $day; Year: $year <br/>\n ";

An error under PHP 7
Fatal error:uncaught error:call to undefined function split () In/tmp/4d38c290-b4cb-43f5-846a-9fa90784a090/code:4
Stack Trace:
#0 {main}
Thrown In/tmp/4d38c290-b4cb-43f5-846a-9fa90784a090/code on line 4

Return to normal under PHP 5.6
month:04; day:30; year:1973

The first argument to split is a regular expression, which means that if you want to match a special character, you need to escape it.

The code is as follows Copy Code

$arr = ' 2016\8\11 ';
$rearr = Split (' [/\] ', $arr);
Var_dump ($rearr)

/*
Array (3) {
[0]=>
String (4) "2016"
[1]=>
String (1) "8"
[2]=>
String (2) "11"
}
*/

It is also because you want to use the syntax of regular expression pattern, the search speed is not very fast.

The Preg_split () function uses Perl-compatible regular expression syntax, and is usually a faster alternative than split (). If you do not need the power of regular expressions, use explode () faster, so that you do not incur the waste of the regular expression engine

The potential for efficiency is caused by the fact that PHP 7 has simply given up the function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.