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)
Grammar Detailed explanation:
Parameter | Description |
---|---|
separator | Required. Specifies where to split the string. |
string | Required. The string to split. |
limit | Optional. 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 = 'one|two|three|four'; // 返回包含一个元素的数组 print_r(explode('|', $str)); // 数组元素为 2 print_r(explode('|', $str, 2)); // 负数的 limit(自 PHP 5.1 起),删除最后一个数组元素 print_r(explode('|', $str, -1)); ?>
Code running result:
[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!