PHP Array and string conversion implementation Method _php Tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
$array =explode (separator, $string);
$string =implode (glue, $array);


The key to using and understanding these two functions is the delimiter (separator) and the glue (glue) relationship. When you convert an array to a string, you will set the glue--the character or code that will be inserted between the array values in the generated string.

Instead, when you convert a string to an array, you specify the delimiter, which is used to mark what should become a separate element. For example, start with a string:

$s 1= ' mon-tue-wed-thu-fri ';
$days _array=explode ('-', $s 1);
The $days _array variable is now an array of 5 elements whose elements, Mon, have an index of 0,tue of 1, and so on.
$s 2=implode (', ', $days _array);
$s 2
The variable is now a comma-delimited list of days in one weeks: Mon,tue,wed,thu,fri

Example 1. Explode () example

Copy CodeThe code is as follows:
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:
$str = ' One|two|three|four ';
A positive limit
Print_r (Explode (' | ', $STR, 2));
Limit of negative numbers
Print_r (Explode (' | ', $STR,-1));
?>

The above example outputs:
Array
(
[0] = one
[1] = Two|three|four
)
Array
(
[0] = one
[1] =
[2] = three
)

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

http://www.bkjia.com/PHPjc/326701.html www.bkjia.com true http://www.bkjia.com/PHPjc/326701.html techarticle Copy the code as follows: $array =explode (separator, $string); $string =implode (glue, $array); The key to using and understanding these two functions is the delimiter (separator) and the glue character (g ...

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