Codectionary / Developer documentation / PHP

Split and Join Text with explode and implode

Convert simple separated text into a list and join values back into readable text.

What it is and why to use it

A small settings value such as "PHP|JavaScript|Ruby" contains several pieces of text separated by a known marker. explode() splits on that literal separator and returns strings. implode() performs the opposite kind of operation by joining array values with a chosen separator. These helpers are useful when the format is genuinely simple and has no quoting or escaping rules.

How it works

explode takes the separator first and the input text second. A positive limit caps the number of returned parts; the last part contains the unsplit remainder. An empty separator throws ValueError in PHP 8. implode takes the separator first and the array second, preserving value order but ignoring keys. Empty fields remain present after splitting unless you deliberately remove them.

Common mistakes and limitations

Do not split CSV with explode when quoted fields can contain commas; use fgetcsv or str_getcsv. Do not use implode to interpolate untrusted values into SQL. Parameters belong in prepared statements. The old reversed argument order for implode is not supported in PHP 8.

Run the examples locally

Save each complete example in its own example.php file and run php example.php in a terminal from that folder. Each example is independent: do not paste both into one file, especially when they declare classes with the same names. Output appears in the terminal. The examples use PHP 8.0-compatible syntax; use a currently supported PHP release for development.

The <?php opening tag starts PHP code. declare(strict_types=1) requests strict scalar argument checks for function calls made from this file; it does not validate external input for you. echo writes output, and PHP_EOL adds the platform’s line ending. Quotation marks delimit strings; they are not part of the printed text unless explicitly included in the string.

These examples use built-in PHP language features and standard extensions. They need no Composer packages, database server, browser APIs or network access. Codectionary’s PHP preview does not execute PHP; use your local interpreter to reproduce the output.

Check your interpreter, lint and run one saved example

php -v
php -l example.php
php example.php

Continue learning

Use the related guides to connect this operation to the surrounding language features. Practise by changing the example inputs and predicting the output before running it.

Syntax

$parts = explode('|', $text);
$text = implode(', ', $parts);

Examples

Turn a simple setting into display text

Split on a literal pipe and join with a human-readable separator.

<?php
declare(strict_types=1);

$setting = 'PHP|JavaScript|Ruby';
$languages = explode('|', $setting);
echo count($languages), PHP_EOL;
echo implode(', ', $languages), PHP_EOL;
  1. The two pipe characters divide the setting into three strings.
  2. count reports the number of list items, while implode inserts a comma and space between them.
  3. No characters inside the language names are transformed. This works for a trusted simple setting, not arbitrary structured data.
  4. Expected output: 3 PHP, JavaScript, Ruby

Split only the first separator

Keep the rest of a note intact even when it contains additional colons.

<?php
declare(strict_types=1);

$line = 'status:review:needs examples';
[$field, $value] = explode(':', $line, 2);
echo $field, PHP_EOL;
echo $value, PHP_EOL;
  1. A positive limit of 2 allows at most two returned parts.
  2. Array destructuring assigns the first part to field and the unsplit remainder to value.
  3. A real parser should first check that the separator exists before destructuring two entries. Here the fixed sample guarantees it.
  4. Expected output: status review:needs examples

Best practices

  • Use a parser when the format has quoting or escaping rules.
  • Choose explicitly whether empty fields are meaningful.

Runtime & versions

PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

PHP Manual

Specifications & further reading