Application of explode () function in PHP

Source: Internet
Author: User
Tags php explode
In PHP, the explode () function divides a string into arrays. The return value of this is an array of strings, each of which is a substring separated by separator as a boundary point. This article describes the application of the explode () function in PHP.

Explode () function introduction

The explode () function splits a string into arrays.

Grammar:

Explode (separator,string,limit).

Separator, required. Specifies where to split the string.

string, required. The string to split.

Limit, optional. Specifies the maximum number of array elements that are returned.

This function returns an array of strings, each of which is a substring separated by separator as a boundary point.

The separator parameter cannot be an empty string. If separator is an empty string (""), Explode () returns FALSE. If separator contains a value that is not found in a string, explode () returns an array containing a single element in the string.

If the limit parameter is set, the returned array contains a maximum of limit elements, and the last element will contain the remainder of the string.

If the limit parameter is a negative number, all elements except the last-limit element are returned. This feature is new in PHP 5.1.0.

Program List:explode () example

    <?php    //Example 1    $fruit  = "Apple Banana Orange Lemon Mango Pear";    $fruitArray = Explode ("", $fruit);    echo $fruitArray [0];//Apple    echo $fruitArray [1];//Banana    //Example 2    08    $data = "gonn:*:nowamagic:1000::/home/foo:/bin/sh";    The    list ($user, $pass, $uid, $gid, $gecos, $home, $shell) = Explode (":", $data);    echo    $user;//Gonn    echo $pass;//*    ?>

Program Run Result:

1    Apple    2    Banana    3    gonn    4    *

Program List: Explode () example using the limit parameter

1    <?php    2    $str = ' one|two|three|four ';    3    //Positive limit    4    print_r (Explode (' | ', $STR, 2));    5    //negative limit (since PHP 5.1)    6    print_r (Explode (' | ', $STR,-1));    7    ?>

Program Run Result:

        [0] = one,        [1] = Two|three|four    )    Array    of        [0] = one        [1] = [        2] = three    11    )

Program List: Converts a string into an array of key values

01 <?php//converts pure string into a trimmed keyed array (f) Unction String2keyedarray ($string, $delimiter = ', ', $kv = ' = ')} {($a = explode ($delimiter, $string)) { Create Parts-foreach ($a as $s) {//each part if ($s) {if ($pos = Strpos ($s, $kv))    {//Key/value delimiter $ka [Trim (substr ($s, 0, $pos)] = Trim (substr ($s, $pos + strlen ($kv)));    Delimiter} else {//key found $ka [] = trim ($s);    One} and the return $ka;    *//String2keyedarray $string = ' a=>1, b=>23, $a, c=>45%, True, D=>ab C ';    Print_r (String2keyedarray ($string)); 

Program Run Result:

1    Array    2    (    3        [A] = 1    4        [b] =    5        [0] = $a    6        [c] = = 45%< C11/>7        [1] = True    8        [d] = ab C    9    )

Summarize:

This article uses a few examples to detail the application of explode () function in PHP, more intuitive for everyone to better understand! Hope to help you in your work!

Related recommendations;

A detailed definition of the explode () function in PHP


Example code for the explode () function in PHP


php explode () function and implode () function usage instructions

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.