Differences between explode and split functions in PHP

Source: Internet
Author: User
Tags expression engine
As we all know, both explode and split can convert strings into arrays through specific characters in php. so why are there two functions of explode and split in the same way, so what is the difference between explode and split? let's take a look at the following small series. I. Preface

The reason for doing this is that these two functions are very similar in that they convert strings into arrays.

II. explode

The following example shows that the generated array has a corresponding order.

$ Pizza = "piece1 piece2 piece3 piece4 piece5 piece6"; $ pieces = explode ("", $ pizza); echo $ pieces [0]; // piece1echo $ 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; // fooecho $ pass ;//*

Note that if the first parameter is a null string, a Warning is generated.

var_dump( explode('','asdasd') ); //Warning: explode(): Empty delimiter in /tmp/e80c9663-e392-4f81-8347-35726052678f/code on line 3//bool(false)

III. split

(PHP 4, PHP 5)

Split-use regular expressions to split strings into arrays

Note that there is no PHP 7 above, that is, 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
\ N "; // 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 normal Month: 04; Day: 30; Year: 5.6

The first parameter of split is a regular expression. that is to say, 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) "11"}*/

It is precisely because the regular expression pattern syntax is used that the search speed is not very fast.

The preg_split () function uses Perl-compatible regular expression syntax, which is usually a faster alternative than split. If you do not need the power of a regular expression, the use of explode () is faster, so that the regular expression engine will not be wasted.

The possible cause for efficiency is that PHP 7 directly abandons this function.

IV. Summary

The above is a summary of all the differences between the explode function and the split function in PHP. I hope this will be helpful for your learning and work.

For more information about the differences between explode and split functions in PHP, see The PHP Chinese website!

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.