PHP explode and implode use instructions _php tips

Source: Internet
Author: User
Tags php and php explode
When it comes to PHP, functions are often the main, but also PHP enthusiasts, and preferences of the Friends of PHP to control the Dongdong, learning PHP Huubang also know that the array is also necessary to grasp, to that way, advanced PHP, is to learn the array in PHP and in the array is usually used to split 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 help!
The use 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 separator of strings as the boundary surface.

If you set the limit parameter, the returned array includes at least limit elements, and the last one will include the rest 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 an array containing the string two-element-meat.
If the limit parameter is a negative number, then go to everything except the last limit element. Pete Sex is new in the PHP 5.1.0.

Because of 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 the PHP 4.0.1.

Example 1.
Explode () example
Copy Code code 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 Code code as follows:

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

The above example outputs: Array ([0] => one [1] => Two|three|four) Array ([0] => one [1] => two [2] => three)
Note: When the function can be safely used for two-build 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 the elements of an array into a single string.
Grammar implode (Separator,array)
Parameter description Separator Optional. Delimits the contents of the lets between the elements of an array. Acquiescence is "" (empty string). Array must be, 400 telephone. An array to union as a string. Clarified that separator parameters are optional. But for the sake of compatibility behind, nominate you to use two parameters.
Reminders and body text: implode () can accept two types of parameter order. But because of historical reasons, explode () is not stop. You must ensure that the separator parameter is only before the string parameter.

PHP's Explode and implode
Copy Code code as follows:

<?php
$zongzi = "1|2|3|4|5|6";
$zongzi = Explode ("|", $zongzi);
Var_dump ($zongzi);
$zongzi = Implode ("|", $zongzi);
Echo $zongzi;
?>
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.