php function explode and split the specific description of the difference

Source: Internet
Author: User
Tags expression engine

Objective

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

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) = Explode (":", $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)

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-B4 Cb-43f5-846a-9fa90784a090/code:4stack Trace: #0 {main}  thrown In/tmp/4d38c290-b4cb-43f5-846a-9fa90784a090/code On line 4//php 5.6 returns 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.

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.