Home >Backend Development >PHP Tutorial >Detailed explanation of the explode() function in PHP

Detailed explanation of the explode() function in PHP

autoload
autoloadOriginal
2021-04-21 10:31:516106browse

  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 = ?   )

    $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);
输出:Array( [0] => 良人当归即好 [1] => 人生当苦无妨  [2] => 我有一剑  [3] => 可搬山)

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);
输出:
Array(   [0] => 良人当归即好      [1] => 人生当苦无妨   [2] => 我有一剑   [3] => 可搬山)
Array(   [0] => 良人当归即好,人生当苦无妨,我有一剑,可搬山)
Array(   [0] => 良人当归即好      [1] => 人生当苦无妨)

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!

Statement:
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