Home > Backend Development > PHP Tutorial > Detailed explanation of php explode() function example

Detailed explanation of php explode() function example

怪我咯
Release: 2023-03-07 20:34:01
Original
2628 people have browsed it

php What does the explode() function do?

php explode() function is: Use one string to split another string and return an array composed of strings.

Now that we understand what the explode() function does, let’s take a look at the syntax:

Grammar

explode(separator,string,limit)
Copy after login

Grammar Detailed explanation:

ParameterDescription
separator Required. Specifies where to split the string.
stringRequired. The string to split.
limitOptional. Specifies the maximum number of array elements returned.

Description
This function returns an array composed of strings, each element of which is represented by a separator A substring divided as a boundary point.

separator parameter cannot be an empty string. If separator is the empty string (""), explode() returns FALSE. If separator contains a value that is not found in string, explode() returns an array containing a single element from string.

If the limit parameter is set, the returned array contains at most limit elements, and the last element will contain the remainder of the 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.
Tips and Notes
Note: The parameter limit was added in PHP 4.0.1.

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

Example

<?php
$str = &#39;one|two|three|four&#39;;
//  返回包含一个元素的数组
print_r(explode(&#39;|&#39;, $str));
// 数组元素为 2
print_r(explode(&#39;|&#39;, $str, 2));

// 负数的 limit(自 PHP 5.1 起),删除最后一个数组元素
print_r(explode(&#39;|&#39;, $str, -1));
?>
Copy after login

Code running result:

Detailed explanation of php explode() function example

[Related article recommendations]

1.PHP splitting and synthesizing string function analysis

The above is the detailed content of Detailed explanation of php explode() function example. For more information, please follow other related articles on the PHP Chinese website!

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