PHP explode () function

Source: Internet
Author: User
Tags php explode
(PHP 3, PHP 4, PHP 5) explode--use a string to split another string description
Array explode (string separator, string string [, int limit])
This function returns an array of strings, each of which is a substring of string, separated by the separator of the strings as the boundary points.

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 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 of 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.

For historical reasons, although implode () can receive two parameter sequences, explode () does not work. You must ensure that the separator parameter is not preceded by a string parameter.

Note: The parameter limit is added in PHP 4.0.1.

Example 1.
Explode () example

Example 1

$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; // *


Example 2.
Example of the limit parameter

$str = ' One|two|three|four '; Positive.
Limit Print_r (Explode (' | ', $STR, 2)); of negative numbers
Limit Print_r (Explode (' | ', $str,-1));

The above example outputs:

Array ([0] = one [1] = one | three | four) array ([0] = one [1] = [2] = three)

Note: This function can be used safely with binary objects.

  • 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.