Using PHP explode() to Split Strings: Syntax, Parameters, and Practical Examples

This article explains the PHP explode() function, covering its syntax, parameters, and three practical examples that demonstrate splitting strings by spaces, commas with a limit, and empty strings to produce arrays.

php Courses
php Courses
php Courses
Using PHP explode() to Split Strings: Syntax, Parameters, and Practical Examples

In PHP, strings are a common data type, and the explode() function allows you to split a string by a specified delimiter into an array. This article introduces the usage of explode() and provides code examples to help you understand it better.

Basic syntax of explode()

explode(string $separator, string $string, int $limit = PHP_INT_MAX): array

The function accepts three parameters: $separator – the delimiter. $string – the string to be split. $limit – optional, limits the number of array elements returned (default is PHP_INT_MAX).

The main purpose of explode() is to split a string by the given separator and return an array where each element is a substring.

Example 1: Split a string by spaces into an array

Code example:

$str = "Hello World";
$result = explode(" ", $str);
print_r($result);

Output:

Array
(
    [0] => Hello
    [1] => World
)

This example splits the string "Hello World" by a space, resulting in the array ["Hello", "World"].

Example 2: Split a string by commas and limit the number of elements

Code example:

$str = "apple,banana,orange,grape";
$result = explode(",", $str, 2);
print_r($result);

Output:

Array
(
    [0] => apple
    [1] => banana,orange,grape
)

This example splits the string "apple,banana,orange,grape" by commas, limiting the result to two elements, yielding ["apple", "banana,orange,grape"].

Example 3: Split a string by an empty string into individual characters

Code example:

$str = "Hello";
$result = explode("", $str);
print_r($result);

Output:

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

This example shows that using an empty string as the separator splits the string into its individual characters.

Summary

By using the explode() function, you can easily split a string by a specified delimiter and obtain an array of substrings, which is useful for text processing, URL parsing, and many other scenarios. Applying explode() appropriately can improve code efficiency and readability.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHPTutorialstring-splittingexplode
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

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.