Splitting and Joining Strings
Convert between strings and arrays with explode, implode, and chunk_split.
explode() and implode()
The two most important string-array conversion functions:
explode($delimiter, $string)— split string into arrayimplode($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']All lessons in this course
- String Basics and Concatenation
- Searching and Replacing in Strings
- Substring and Padding Functions
- Splitting and Joining Strings