Example
Break the string into an array:
<?php$str = "www.php.cn";print_r (explode(".",$str));?>
Definition and usage
explode() function uses one string to split another string and returns Array of strings.
Note: The "separator" parameter cannot be an empty string.
Note: This function is binary safe.
Syntax
explode(separator,string,limit)
<? // ### 切分字符串 #### function jb51netcut($start,$end,$file){ $content=explode($start,$file); $content=explode($end,$content[1]); return $content[0]; } ?>
explode definition and usage
explode() function splits a string into an array.
This function returns an array composed of strings, each element of which is a substring separated by separator 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.
In this example, we will split the string into an array:
<?php $str = "Hello world. It's a beautiful day."; print_r (explode(" ",$str)); ?>
Output:
Array ( [0] => Hello [1] => world. [2] => It's [3] => a [4] => beautiful [5] => day. )
Parameters Description
separator Required. Specifies where to split the string.
string Required. The string to split.
limit Optional. Specifies the number of array elements to be returned.
Possible values:
Greater than 0 - Returns an array containing at most limit elements
Less than 0 - Returns an array containing all but the last -limit elements Array
0 - will be treated as 1, returns an array containing one element
Technical detailsReturn value:
Return String array.
Use the limit parameter to return some array elements:
<?php $str = 'one,two,three,four'; // 返回包含一个元素的数组 print_r(explode(',',$str,0));print "<br>"; // 数组元素为 2 print_r(explode(',',$str,2));print "<br>"; // 删除最后一个数组元素 print_r(explode(',',$str,-1)); ?>
The above is the detailed content of PHP function explode() that splits a string and returns an array composed of strings. For more information, please follow other related articles on the PHP Chinese website!