PHP explode and implode usage instructions

Source: Internet
Author: User
Tags php explode

Speaking of php, the function is very long and important, and it is also a php enthusiast, and friends who prefer php advanced must control the stuff, learning php friends also know that the array is also necessary to grasp, in that way, advanced php is to learn arrays in php. In arrays, we usually need to use split strings or something, so we need to use the explode and implode functions, let's talk about the use of explode and implode in php, ballet shoes, and hope to help friends and friends!
Usage of explode in PHP
(PHP 3, PHP 4, PHP 5) explode -- use one string to separate the description of another string
Array explode (string separator, string [, int limit])
This function returns an array composed of strings. Each element is a substring of a string, which is separated by a string separator as an edge interface.

If the limit parameter is set, the returned array includes at least limit elements, and the final metadata includes the rest of the string.
If separator is a null string (""), explode () goes to FALSE.
If the values included in the separator are not found in the string, the explode () will go to the array of double-element offsets of the pregnant string.
If the limit parameter is a negative number, all the metadata except the last limit element is used. This feature is added in PHP 5.1.0.

Because of historical reasons, although implode () can accept the order of two types of parameters, but explode () does not stop. You must ensure that the separator parameter is prior to the string parameter.

Note: The limit parameter is in PHP 4.0.1.

Example 1.
Explode () Example
Copy codeThe 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.
Limit parameter example
Copy codeThe Code is as follows:
<? Php
$ Str = 'one | two | three | four '; // positive number
Limit print_r (explode ('|', $ str, 2); // negative
Limit print_r (explode ('|', $ str,-1 ));
?>

The above example will output: Array ([0] => one [1] => two | three | four) array ([0] => one [1] => two [2] => three)
Note: When a function can be safely used for two-in-process mirroring.
PHP implode () function

Example <? Php $ arr = array ('hello', 'World! ', 'Beautiful', 'day! '); Echo implode ("", $ arr);?>
Output:
Hello World! Beautiful Day!

The subscription and usage implode () function combines array elements into a string.
Syntax implode (separator, array)
Optional. Specifies the content that is left between elements of the array. The default value is "" (Null String ). Array is required. Phone number 400. Array to be joined as a string. Clarify that the separator parameter is optional. However, for compatibility purposes, we recommend that you use two parameters.
Reminder and body text: implode () can accept the order of two types of parameters. However, for historical reasons, explode () is endless. You must ensure that the separator parameter is not stopped until the string parameter is set.

PHP explode and implode
Copy codeThe 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.