Backend Development 4 min read

Using PHP explode() to Split Strings into Arrays

This article explains how to use PHP's built-in explode() function to split strings into arrays, covering syntax, optional limit parameter, and multiple examples including comma-separated lists, space-separated words, and a note on using str_split for character-level splitting.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP explode() to Split Strings into Arrays

In PHP development, you often need to split a string into an array using a specific delimiter. The built‑in explode function provides a simple way to achieve this conversion.

The basic syntax of explode is:

array explode ( string $delimiter , string $string [, int $limit ] )

The $delimiter parameter specifies the separator, $string is the input string, and the optional $limit limits the number of array elements returned.

Example 1 splits a comma‑separated list of fruit names:

<?php
$str = "Apple,Banana,Orange,Strawberry";
$delimiter = ",";
$fruits = explode($delimiter, $str);

print_r($fruits);
?>

The output is:

Array
(
    [0] => Apple
    [1] => Banana
    [2] => Orange
    [3] => Strawberry
)

Example 2 uses a space as the delimiter to split a sentence into words:

<?php
$str = "Hello World! How are you today?";
$delimiter = " ";
$words = explode($delimiter, $str);

print_r($words);
?>

The resulting array contains each word as an element:

Array
(
    [0] => Hello
    [1] => World!
    [2] => How
    [3] => are
    [4] => you
    [5] => today?
)

If you need to split a string into individual characters, use str_split instead of explode :

<?php
$str = "Hello";
$characters = str_split($str);

print_r($characters);
?>

The output shows each character as a separate array element:

Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
)

This article demonstrated how to use PHP's explode function for string‑to‑array conversion with various delimiters and highlighted the alternative str_split for character‑level splitting.

backendPHParrayexplodestring split
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.