Summary of differences between explode function and Split function in PHP

Source: Internet
Author: User
Tags php explode expression engine
First, preface

The reason for doing this is because the two functions are very similar in that they are converting strings into groups.

Second, explode

As can be seen from the following example, the resulting array is in the 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) = Expl Ode (":", $data); Echo $user; Fooecho $pass; // *

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

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-using regular expressions to split a string into an array

Note that there is no PHP 7 above, 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 "; PHP 7 Error fatal error:uncaught Error:call to undefined function split () in/tmp/4d38c290-b4cb-43f5-846a-9fa90784a090/c Ode:4stack Trace: #0 {main} thrown In/tmp/4d38c290-b4cb-43f5-846a-9fa90784a090/code on line 4//php 5.6 return to normal month:04; day:30; year:1973

The first argument to split is a regular expression, meaning that if you want to match special characters, you need to escape.

$arr = ' 2016\8\11 '; $rearr = Split (' [/\] ', $arr); Var_dump ($rearr)/*array (3) {[0]=> string (4) "[1]=> string (1) "8" [2]=> string (2) "11"}*/

It is also because the syntax of the regular expression pattern is used, so the search is not fast.

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

Perhaps the reason for the efficiency is that PHP 7 gave up the function directly.

Iv. Summary

The above is summed up in PHP explode function and split function of the whole content, hope that everyone's study and work can be helpful.

Related Article

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.