Detailed explanation of the explode() function in PHP

autoload
Release: 2023-04-09 21:44:01
Original
6077 people have browsed it

  Detailed explanation of the explode() function in PHP

                                                                                                                                                                                    ##PHP provides us with the explode() function. This article will take you to take a look. First of all, the first thing that should be understood should be the syntax:

explode ( string $delimiter,string $string , int $limit = ?   )
Copy after login

    $delimiter: The delimiter character on the boundary.
  • #$string: Input string.
  • $limit: If the value is a positive number, the returned array contains up to $limit elements, and the last element will contain the remainder of $string. If the value is negative, all but the last -$limit elements are returned. If $limit is 0, it will be treated as 1.
  • Return value: array type array, each element is a substring of $string.
  • Code example:

1. When using two parameters:

<?php
$sentence1 = "良人当归即好,人生当苦无妨,我有一剑,可搬山";
$sentence2 = explode(",", $sentence1);
print_r($sentence2);
Copy after login
输出:Array( [0] => 良人当归即好 [1] => 人生当苦无妨  [2] => 我有一剑  [3] => 可搬山)
Copy after login

2. When there are three parameters used:

<?php
$sentence1 = "良人当归即好,人生当苦无妨,我有一剑,可搬山";

//$limite为正整数
$sentence2 = explode(",", $sentence1,3);
print_r($sentence2);
echo "<br>";

//$limite为0
$sentence3 = explode(",", $sentence1,0);
print_r($sentence3);
echo "<br>";

//$limite为负整数
$sentence4 = explode(",", $sentence1,-2);
print_r($sentence4);
Copy after login
输出:
Array(   [0] => 良人当归即好      [1] => 人生当苦无妨   [2] => 我有一剑   [3] => 可搬山)
Array(   [0] => 良人当归即好,人生当苦无妨,我有一剑,可搬山)
Array(   [0] => 良人当归即好      [1] => 人生当苦无妨)
Copy after login
Recommendation:

2021 PHP interview questions summary (collection)》《php video tutorial

The above is the detailed content of Detailed explanation of the explode() function in PHP. 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