PHP explode() Function
The explode() function splits a string into an array using a delimiter.
Syntax
explode(string $separator, string $string, int $limit = PHP_INT_MAX): array
Parameters
| Parameter | Type | Description |
|---|---|---|
$separator | string | The delimiter to split by |
$string | string | The string to split |
$limit | int | Max number of elements (optional) |
Return Value
Returns an array of strings created by splitting the input string.
Try It Online
Output
Click Run to execute your code
More Examples
Split CSV Data
<?php
$csv = "apple,banana,cherry";
$fruits = explode(",", $csv);
print_r($fruits);
// ["apple", "banana", "cherry"]
?>
Split Words
<?php
$sentence = "Hello World PHP";
$words = explode(" ", $sentence);
print_r($words);
// ["Hello", "World", "PHP"]
?>
Limit Splits
<?php
$str = "one-two-three-four";
$parts = explode("-", $str, 2);
print_r($parts);
// ["one", "two-three-four"]
?>
Tip: Use
implode() to join array elements back into a string.
Common Use Cases
- Parsing CSV data
- Splitting URL paths
- Processing form input
- Breaking text into words
Related Functions
- implode() - Join array to string
str_split()- Split into character chunkspreg_split()- Split with regex
Enjoying these tutorials?