Home > Backend Development > PHP Tutorial > Detailed explanation of the instance usage of PHP functions explode and implode

Detailed explanation of the instance usage of PHP functions explode and implode

伊谢尔伦
Release: 2023-03-11 16:08:02
Original
1890 people have browsed it

1. php stringSplit functionexplode()

explode() usage:

array explode(string separator,string string [,int limit])

explode() description:

This function returns an array composed of strings. Each element is a substring of string. They are used as string separators. Boundary points are segmented.

If the limit parameter is set, the returned array contains up to limit elements, and the last element will contain the remainder of the string.

If separator is an empty string (""), explode() will return FALSE.

If the value contained in the separator is not found in the string, then explode() will return an array containing a single element of the string.

If the limit parameter is a negative number, all elements except the last limit elements are returned.

explode() example

<?php
//示例 1
$pizza = “piece1 piece2 piece3 piece4 piece5 piece6″;
$pieces = explode(” “, $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
?>
Copy after login
<?php
//示例 2
$data = “foo:*:1023:1000::/home/foo:/bin/sh”;
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(“:”, $data);
echo $user; // foo
echo $pass; // *
?>
Copy after login
<?php
//limit 参数示例
$str = ‘one|two|three|four’; // 正数的
limit print_r(explode(‘|’, $str, 2)); // 负数的
limit print_r(explode(‘|’, $str, -1));
?>
Copy after login

The above example will output:

Array ([0] => one [1] => two|three|four ) Array ( [0] => one [1] => two [2] => three)
Copy after login

Note: This function is safe for use with binary objects.

2. PHP string merging function implode()

implode() usage:

array implode(string separator,string array)

implode() Description:

implode() function combines array elements into a string.

separator is optional. Specifies what is placed between array elements. Default is "" (empty string).

array required.

implode() example:

<?php
$arr = array(‘Hello’,’World!’,’Beautiful’,’Day!’);echo implode(” “,$arr);
?>
Copy after login

Output:

Hello World! Beautiful Day!

Note: implode() can receive two parameter orders; But explode() does not work, you must ensure that the separator parameter precedes the string parameter.

The above is the detailed content of Detailed explanation of the instance usage of PHP functions explode and implode. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template