Backend Development 4 min read

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:

<code>array explode(string $delimiter, string $string, int $limit = PHP_INT_MAX)</code>

Example 1 – splitting a comma-separated string:

<code>&lt;?php
$str = "Hello,World,PHP";
$arr = explode(",", $str);
print_r($arr);
?&gt;</code>

Output:

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

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

<code>&lt;?php
$str = "Hello|World|PHP";
$arr = explode("|", $str);
print_r($arr);
?&gt;</code>

Result is the same three-element array.

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

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

Output:

<code>www</code>

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.

backendPHPTutorialstring-manipulationexplode
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.