Understanding PHP explode() Function: Syntax, Examples, and Use Cases

This article explains PHP's explode() function, detailing its syntax, parameters, and optional limit, and provides multiple code examples demonstrating string-to-array splitting with various delimiters and extracting URL components, helping developers effectively manipulate strings in backend applications.

php Courses
php Courses
php Courses
Understanding PHP explode() Function: Syntax, Examples, and Use Cases

PHP is a widely used server-side scripting language with powerful string handling capabilities; this article focuses on its built-in explode() function.

The explode() function splits a string into an array based on a delimiter, accepting three parameters: the delimiter, the input string, and an optional limit (default PHP_INT_MAX).

Basic syntax:

array explode(string $delimiter, string $string, int $limit = PHP_INT_MAX)

Example 1 – splitting a comma-separated string:

<?php
$str = "Hello,World,PHP";
$arr = explode(",", $str);
print_r($arr);
?>

Output:

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

Example 2 – using a multi-character delimiter (vertical bar):

<?php
$str = "Hello|World|PHP";
$arr = explode("|", $str);
print_r($arr);
?>

Result is the same three-element array.

Example 3 – extracting a domain name from a URL by splitting on the dot character:

<?php
$url = "https://www.example.com";
$arr = explode(".", $url);
$domain = $arr[1];
echo $domain;
?>

Output: www In summary, explode() is a versatile function for converting strings to arrays or retrieving specific parts of a string, making it essential for backend PHP development.

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.

BackendPHPexplode
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.