Instructions for using the PHP explode () function and the implode () function

Source: Internet
Author: User
Tags php explode
Explode and implode in PHP, ballet shoes, look forward to the Huubang to help!

Speaking of PHP, the function is often the main, is also PHP enthusiasts, and like PHP advanced friends must control the east, learning PHP Huubang also know that the array is also necessary to grasp, can that way, advanced PHP, Also is to learn the array in PHP and in the array is usually used to divide the string ah what, it will be used to explode, and implode function, the next said in PHP explode and implode usage, ballet shoes, hope for the Huubang have some assistance!
Usage of explode in PHP
(PHP 3, PHP 4, PHP 5) explode--use a string to split another string description
Array explode (string separator, string string [, int limit])
The function goes to an array of strings, each of which is a substring of string, separated by the string separator as the boundary polygon.

If the limit parameter is set, the array to be returned includes a minimum of limit elements, and the last element will contain the remainder of the string.
If separator is an empty string (""), explode () will go to FALSE.
If the value included in the separator is not found in the string, then explode () will go to the array containing string two-element meat.
If the limit parameter is a negative number, go to all the meta-meat except the last limit element. Pete Sex is new in PHP 5.1.0.

For historical reasons, although implode () can accept two kinds of parameter order, but explode () does not stop. You must ensure that the separator parameter is not preceded by a string parameter.

Note: The parameter limit is participating in PHP 4.0.1.

Example 1.
Explode () example

The code is as follows:

<?php///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

The code is as follows:

<?php $str = ' one|two|three|four '; Positive limit Print_r (Explode (' | ', $STR, 2)); Negative limit Print_r (Explode (' | ', $STR,-1));?>

The above example outputs: Array ([0] = one [1] = Two|three|four) Array ([0] = one [1] = [2] = three)
Note: When the function can be used safely both into the building for the elephant.
PHP implode () function

Example <?php$arr = Array (' Hello ', ' world! ', ' Beautiful ', ' day! '); echo Implode ("", $arr);? >
Output:
Hello world! Beautiful day!

The implode () function combines array elements into a single string.
Grammar implode (Separator,array)
Parameter description Separator Optional. Delimits the contents of put aside between array elements. Acquiescence is "" (empty string). Array must be, 400 telephones. An array to federate as a string. Clarified that although the separator parameter is optional. But for the sake of compatibility, the use of two parameters is recommended.
Reminders and body text: implode () can accept two types of parameter order. But because of historical reasons, explode () is not stopped. You must ensure that the separator parameter is not stopped until the string parameter.

PHP Explode and implode

The code is as follows:

<?php $zongzi = "1|2|3|4|5|6"; $zongzi = Explode ("|", $zongzi); Var_dump ($zongzi); $zongzi = Implode ("|", $zongzi); echo $zongzi;?>

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.