A brief summary of the difference between explode function and Split function in PHP _php tips

Source: Internet
Author: User
Tags explode php explode stack trace

First, the preface

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

Second, explode

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

$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, it is generated Warning .

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

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

Third, split

(PHP 4, PHP 5)

split -Split a 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.

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

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

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

splitThe first argument is a regular expression, that is, if you want to match a special character, you need to escape it.

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

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

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

preg_split()The function uses Perl-compatible regular expression syntax, which is usually a split() faster alternative. If you don't need the power of a regular expression, use it explode() faster, so you don't incur a waste of regular expression engines.

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

Iv. Summary

The above is summed up in PHP explode function and split function of the difference between the whole content, I hope that the study and work can help.

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.