0Pricing
PHP Academy · Lesson

Splitting and Joining Strings

Convert between strings and arrays with explode, implode, and chunk_split.

Splitting and Joining Strings is a free PHP Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

explode() and implode()

The two most important string-array conversion functions:

  • explode($delimiter, $string) — split string into array
  • implode($glue, $array) — join array into string

explode() Basics

Split a string on a delimiter:

<?php
$csv = 'Alice,Bob,Carol,Dave';

$names = explode(',', $csv);
print_r($names);
// ['Alice', 'Bob', 'Carol', 'Dave']

// Limit the number of parts
$parts = explode(',', $csv, 2);
print_r($parts);
// ['Alice', 'Bob,Carol,Dave']

implode() and join()

Join an array of strings into a single string:

<?php
$tags = ['php', 'mysql', 'linux'];

echo implode(', ', $tags);    // php, mysql, linux
echo implode(' | ', $tags);   // php | mysql | linux
echo implode('', $tags);      // phpmysqllinux

// join() is an alias for implode()
echo join('-', $tags);  // php-mysql-linux

Parsing CSV Lines

Use str_getcsv() to parse a CSV line including quoted fields:

<?php
$line = 'Alice,"New York, NY",30';

$fields = str_getcsv($line);
print_r($fields);
// ['Alice', 'New York, NY', '30']

preg_split() — Split on Pattern

Split on a regex pattern for complex delimiters:

<?php
$text = 'one1two2three3four';

// Split on any digit
$parts = preg_split('/[0-9]/', $text);
print_r($parts);
// ['one', 'two', 'three', 'four']

// Split on whitespace (one or more)
$words = preg_split('/\s+/', '  hello   world  ');
print_r(array_filter($words)); // ['hello', 'world']

str_split()

str_split() splits a string into an array of chunks of a given size:

<?php
$str = 'Hello';

// Split into individual characters
$chars = str_split($str);
print_r($chars); // ['H','e','l','l','o']

// Split into chunks of 2
$chunks = str_split('ABCDEF', 2);
print_r($chunks); // ['AB','CD','EF']

Building Query Strings

Use http_build_query() to assemble URL query strings from arrays:

<?php
$params = [
    'search' => 'php tutorial',
    'page'   => 2,
    'sort'   => 'date',
];

$query = http_build_query($params);
echo $query;
// search=php+tutorial&page=2&sort=date

echo '/posts?' . $query;

Parsing Query Strings

Parse a URL query string into an array with parse_str():

<?php
$qs = 'name=Alice&age=30&city=London';

parse_str($qs, $params);
print_r($params);
// ['name'=>'Alice', 'age'=>'30', 'city'=>'London']

// Or use parse_url + parse_str for full URLs
$url    = 'https://example.com/search?q=php&page=1';
$query  = parse_url($url, PHP_URL_QUERY);
parse_str($query, $params);
echo $params['q']; // php

implode for SQL IN Clauses

A classic use of implode — building SQL IN clauses from arrays:

<?php
$ids = [1, 2, 3, 4, 5];
$placeholders = implode(',', array_fill(0, count($ids), '?'));

$sql = "SELECT * FROM users WHERE id IN ($placeholders)";
echo $sql;
// SELECT * FROM users WHERE id IN (?,?,?,?,?)
// Use prepared statements with these placeholders!

String to Words Array

Split a sentence into words and process each:

<?php
$sentence = 'The quick brown fox';
$words = explode(' ', $sentence);

$reversed = array_reverse($words);
echo implode(' ', $reversed);
// fox brown quick The

$wordCount = count($words);
echo "Word count: $wordCount"; // 4

sprintf with Array Spread

Combine implode with sprintf for flexible formatting:

<?php
$cols   = ['name', 'email', 'age'];
$values = ['Alice', 'alice@test.com', 30];

$colList = implode(', ', $cols);
$valPlaceholders = implode(', ', array_fill(0, count($values), '?'));

$sql = "INSERT INTO users ($colList) VALUES ($valPlaceholders)";
echo $sql;
// INSERT INTO users (name, email, age) VALUES (?, ?, ?)

Quick Check

What does explode(',', 'a,b,c', 2) return?

Recap: Splitting and Joining

Key functions:

  • explode($delim, $str) — string to array
  • implode($glue, $arr) — array to string
  • str_split($str, $len) — split into fixed chunks
  • preg_split($pattern, $str) — split on regex
  • str_getcsv() — parse CSV with quoted fields
  • http_build_query() — array to URL query string

Frequently asked questions

Is the “Splitting and Joining Strings” lesson free?

Yes — the full text of “Splitting and Joining Strings” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Splitting and Joining Strings”?

Convert between strings and arrays with explode, implode, and chunk_split. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Splitting and Joining Strings” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. String Basics and Concatenation
  2. Searching and Replacing in Strings
  3. Substring and Padding Functions
  4. Splitting and Joining Strings
← Back to PHP Academy