Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

PHP Dictionary

  1. Home
  2. PHP Dictionary
  3. explode() / implode()

explode() / implode() Since: PHP 4(2000)

Splits a string into an array using a delimiter, or joins array elements into a string. Useful in many situations such as processing CSV data and parsing URLs.

Syntax

// Splits a string by a delimiter and returns an array.
explode($separator, $string, $limit);

// Joins array elements into a string.
implode($separator, $array);

// Splits a string into an array of chunks of a given length.
str_split($string, $length);

Functions

FunctionDescription
explode($separator, $string, $limit)Splits $string by $separator and returns an array. $limit sets the maximum number of elements and is optional.
implode($separator, $array)Returns a string formed by joining each element of $array with $separator.
str_split($string, $length)Splits a string into an array of chunks of $length characters each. The default value of $length is 1.

Return Value

explode() and str_split() return an array. implode() returns the joined string.

Sample Code

<?php
// Splits a comma-separated string into an array.
$csv = "apple,orange,grape";
$fruits = explode(",", $csv);
var_dump($fruits); // Outputs array("apple", "orange", "grape").

// Limits the number of splits.
$data = "Yamada:Taro:30:Tokyo";
$parts = explode(":", $data, 3);
var_dump($parts); // Outputs array("Yamada", "Taro", "30:Tokyo"). The remainder is stored as the last element.

// Joins an array into a string.
$words = ["PHP", " is", " fun"];
echo implode("", $words); // Outputs "PHP is fun".

// Joins with a delimiter.
$tags = ["HTML", "CSS", "JavaScript"];
echo implode(", ", $tags); // Outputs "HTML, CSS, JavaScript".

// Builds an SQL IN clause.
$ids = [1, 5, 12, 23];
$in_clause = implode(",", $ids);
echo "SELECT * FROM users WHERE id IN ($in_clause)"; // Builds the IN clause.

// Splits a string into individual characters with str_split().
$chars = str_split("Hello");
var_dump($chars); // Outputs array("H", "e", "l", "l", "o").

// Splits a string into chunks of a specified length with str_split().
$code = "ABCDEFGHIJ";
$chunks = str_split($code, 3);
var_dump($chunks); // Outputs array("ABC", "DEF", "GHI", "J").

// Parses a file path into its segments.
$path = "/var/www/html/index.php";
$segments = explode("/", trim($path, "/"));
var_dump($segments); // Outputs array("var", "www", "html", "index.php").

Overview

explode() and implode() are functions for converting between strings and arrays. They are used extremely often in PHP development for tasks such as processing CSV data, parsing URLs and file paths, and building SQL statements.

Passing a positive integer as the third argument to explode() sets the maximum number of elements; the remaining portion of the string is stored together in the last element. Passing a negative integer excludes that many elements from the end. Note that passing an empty string as the separator causes an error. Use str_split() to split a string character by character.

str_split() splits by character count rather than a delimiter, making it convenient for processing fixed-width data and chunking strings. Use str_replace() to replace substrings and strpos() to search within strings.

If you find any errors or copyright issues, please .