PHP explode() Function
Example
Splitting strings into arrays:
<?php $str = "Hello world. I love Shanghai!"; print_r (explode(" ", $str)); ?>
Definition and Usage
The explode() function splits a string into an array.
Note:"separator"Parameter cannot be an empty string.
Note:This function is binary safe.
Syntax
explode(separator,string,limit)
Parameters | Description |
---|---|
separator | Required. Specifies where to split the string. |
string | Required. The string to be split. |
limit |
Optional. Specifies the number of array elements to return. Possible Values:
|
Technical Details
Return Value: | Returns an array of strings |
PHP Version: | 4+ |
Update Log: | Negative numbers were added in PHP 4.0.1. limit Parameters. Negative numbers were added in PHP 5.1.0. limit support. |
More Examples
Example 1
Usage limit Parameters to return some array elements:
<?php $str = 'one,two,three,four'; // Zero limit print_r(explode(',', $str, 0)); // Positive limit print_r(explode(',', $str, 2)); // Negative limit print_r(explode(',', $str, -1)); ?>