
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
PHP String explode() Function
The PHP String explode() function is used for splitting a string into individual strings. Basically this method separates a string according to a string delimiter and returns an array containing the strings produced by dividing the original string.
Syntax
Below is the syntax of the PHP String explode() function −
array explode ( string $delimiter , string $string [, int $limit ] )
Parameters
The explode() function needs three parameters and two of them are required and one optional. All three parameters are covered below −
$delimiter − This character identifies the critical points or points at which the string will divide; when this character appears in the string, it represents the end of one element of the array and the beginning of another.
$string − It is the input string that will be split into arrays.
$limit − This is optional. It shows the number of elements in the array. This argument can be any integer - positive, negative, or zero.
Return Value
The explode() function returns an array of strings produced by splitting the string given along the separator's limits.
If the separator is an empty string (""), explode() raises a ValueError. If the separator contains a value that is not in the string and the limit is negative, an empty array will be returned; otherwise, an array containing the string will be returned. If separator values appear at the beginning or end of a string, they will be appended as empty array values to the first or last positions of the returned array, correspondingly.
PHP Version
This function introduced in core PHP 4 and continues to work easily in PHP 5, PHP 7, and PHP 8.
Example 1
First we will show you the basic example of the PHP String explode() function to split a string into an array of words.
<?php // A sentence to split $sentence = "Learn PHP easily with examples"; // Split sentence into words using space as a delimiter $words = explode(" ", $sentence); // Print the words print_r($words); ?>
Output
Here is the outcome of the following code −
Array ( [0] => Learn [1] => PHP [2] => easily [3] => with [4] => examples )
Example 2
In the below PHP code we will try to use the explode() function and extracts specified user data from a colon-separated text.
<?php // User data in a colon-separated format $userData = "amit:*:101:100:/home/amit:/bin/bash"; // Split the string into parts list($username, $password, $uid, $gid, $homeDir, $shell) = explode(":", $userData); // Print the extracted details echo "Username: $username\n"; echo "Home Directory: $homeDir\n"; ?>
Output
This will generate the below output −
Username: amit Home Directory: /home/amit
Example 3
This program explains how explode() function handles strings that do not have a delimiter. If the delimiter is not recognized in the string, explode() returns an array with the complete string as the only item. Otherwise, it will divide the string.
<?php // Strings with and without the delimiter $input1 = "welcome"; $input2 = "welcome,home"; // Use explode with a comma as the delimiter print_r(explode(',', $input1)); print_r(explode(',', $input2)); ?>
Output
This will create the below output −
Array ( [0] => welcome ) Array ( [0] => welcome [1] => home )
Example 4
This program explains how to use the limit parameter in explode() function. The limit parameter defines how many splits are made. A positive integer limits the array's size but a negative value eliminates the last items from the result.
<?php // String to split $string = "apple|banana|cherry|date"; // Positive limit print_r(explode('|', $string, 2)); // Negative limit print_r(explode('|', $string, -1)); ?>
Output
Following is the output of the above code −
Array ( [0] => apple [1] => banana|cherry|date ) Array ( [0] => apple [1] => banana [2] => cherry )