Home > Backend Development > PHP Tutorial > Instructions for using PHP's explode and implode

Instructions for using PHP's explode and implode

PHP中文网
Release: 2023-02-28 19:44:02
Original
1154 people have browsed it

Speaking of php, functions are very important. They are also something that php enthusiasts and friends who like to advance php must master. Friends who are learning php also know that arrays must also be mastered. You can do this Tao, advanced PHP, that is, learning arrays in PHP. In arrays, you usually need to split strings or something, so you need to use the explode and implode functions. Here is the usage of explode and implode in PHP. , Ballet Shoes, I hope it will be helpful to my friends!
Usage of explode in PHP
(PHP 3, PHP 4, PHP 5) explode--use one string to split another string description
array explode ( string separator, string string [, int limit] )
This function returns an array composed of strings. Each element is a substring of string. They are divided by the string separator as a boundary surface. Go in.

If the limit parameter is set, the returned array contains at least limit elements, and the last element will contain the remaining part of string.
If separator is an empty string (""), explode() will return FALSE.
If the value contained in separator is not found in string, explode() will return an array containing the two elements of string.
If the limit parameter is a negative number, all elements except the last limit elements are returned. This feature is new in PHP 5.1.0.

Due to historical reasons, although implode() can accept two types of parameter orders, explode() cannot. You must ensure that the separator parameter comes before the string parameter.

Note: The parameter limit is added in PHP 4.0.1.

Example 1.
explode() example

The code is as follows:

<?php 
// 示例 1 $pizza = "piece1 piece2 piece3 piece4 piece5 piece6"; 
$pieces = explode(" ", $pizza); 
echo $pieces[0]; // piece1 
echo $pieces[1]; // piece2 
// 示例 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


Example 2.
limit parameter example

The code is as follows:

<?php 
$str = &#39;one|two|three|four&#39;; // 正数的 
limit print_r(explode(&#39;|&#39;, $str, 2)); // 负数的 
limit print_r(explode(&#39;|&#39;, $str, -1)); 
?>
Copy after login

The above example will output: Array ([0] => one [1] => two|three|four ) Array ( [0 ] => one [1] => two [2] => three)
Note: When the function can be used safely on two objects.
PHP implode() function

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template