Web Analytics

PHP explode() Function

String Function PHP 4+

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

ParameterTypeDescription
$separatorstringThe delimiter to split by
$stringstringThe string to split
$limitintMax 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 chunks
  • preg_split() - Split with regex