PHP Array Functions
explode() Function
Definition and Usage
The explode() function breaks a string into an array.
Note:
The "separator" parameter cannot be an empty string.
Syntax
explode(separator,string,limit)
| Parameter |
Description |
| separator |
Required. Specifies where to break the string |
| string |
Required. The string to split |
| limit |
Optional. Specifies the number of array elements to return. Possible values:
- Greater than 0 - Returns an array with a maximum of limit element(s)
- Less than 0 - Returns an array except for the last -limit elements()
- 0 - Returns an array with one element
|
Example 1
Break a string into an array:
<?php
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>
Run Example>>
Example 2
Using the limit parameter to return a number of 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));
?>
Run Example>>