Table of Contents
How explode() Function works in PHP?
Examples of PHP explode()
Example #4

PHP explode()

Aug 29, 2024 pm 12:55 PM
php

The explode() function of the PHP Programming Language is an inbuilt function which helps in splitting a string into many different strings. The explode() function helps splitting the string based on the string’s delimeter which means that it is going to split the string wherever delimeter character will present/occur. This explode() function will return an array which contains the string/string formed after splitting the string which is original. This PHP explode() function usually accepts only three parameters to split a string into many string elements and stores into an array. It is like cutting the big string into many small strings and so on.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

explode(separator1, OriginalString1, No.ofElements1)
Copy after login

How explode() Function works in PHP?

The explode() function of the PHP Programming Language works based on the three parameters mentioned. They are Separator1, OriginalString1 and No.ofElements1 Parameter. It works by splitting the string into smaller strings with the help of the parameters inside the explode() function. All these smaller string elements are stored in array with index values and also based on the parameters of the explode() function.

Explanation of Parameters:

The Parameters of the explode() function are actually accepting three parameters but in those only two parameters are mandatory and one parameter is an optional parameter.

1. Separator1/Delimeter: The Separator1 Parameter of the PHP Programming Language actually specifies some critical points at which point the string has to split which means that whenever the string character will be found in the string element then it is going to symbolize the end of one array element and start of the another. This delimeter/Separator1 parameter is a mandatory parameter.

2. OriginalString1: The OriginalString1 Parameter of the PHP Programming Language is actually an original string which is used to split a string into many strings and stores in an array only if the string characters are available inside of the string. This OriginalString1 parameter is also a mandatory parameter.

3. No.ofElements1/Limit: This Parameter is an optional parameter and it’s not mandatory. This parameter helps in specifying the number of array elements. This parameter can be of any type of integer.

It can be positive integer, negative integer or the zero integer.

  • Positive Integer (N): If No.ofElements1 parameter is passed with Positive Integer value then it means array will contain this number of string elements. If number of elements after separating the elements w.r.t the separator will emerge as to be the value greater then N-1 elements but remains same and the last string element will be the whole remaining string.
  • Negative Integer (N): If the negative integer is passed to the No.ofElements1 parameter then last string element N will be trimmed and the remaining array element will be returned as only one single string.
  • Zero Integer: If the No.ofElements1 parameter is passed with “Zero” value then the array which is going to return only one string element i.e., Full String (whole string). When no value is passed to this No.ofElements1 parameter then array returned will be going to have total number of elements formed after the string separation with the separator.

Return value Type:

The return value type of the explode() function is the single array with the list of the strings.

Examples of PHP explode()

Given below are the examples mentioned:

Example #1

This is the example of splitting a single string into many small string by taking the space in between the string into the consideration. In the below example inside of the PHP tags, a variable “$s1” is created and assigned a string sentence to the $s1 variable. Then print_r() function is used along with the explode() function in it. Here separator1/delimeter parameter is space “ ”, $s1 is the input parameter/Original String and then here no parameter is mentioned. There is no limit/No.ofElements1 parameter here so there is no limit to splitting of the string. After splitting, inside of the array index values small strings will be stored and printed with the help of the print_r() function.

Syntax:

<?php
$s1 = "My Name is Pavan Kumar Sake";
print_r (explode(" ",$s1));
?>
Copy after login

Output:

PHP explode()

Example #2

This is the example of implementing with limit value “0”. At first, a variable “$stra” is created and this variable is also assigned with a string value ‘car1, bus1, motorbike1, cycle1’. Then print_r() function is made along with the explode() function in it. In the explode() function, “,” is used as separator1 parameter, $stra variable value is used as OriginalString1 Parameter and value “0” is used as No.ofElements1 Parameter. It is mentioned in the parameters description that if the No.ofElements1/Limit value is mentioned as 0 then the whole original string is considered as one single string array element. This will be printed as shown in the output. Then print function is used to print line break.

Syntax:

<?php
$stra = 'car1, bus1, motorbike1, cycle1';
print_r(explode(',',$stra,0));
?>
Copy after login

Output:

PHP explode()

Example #3

This is the example of implementing the string splitting with the help of the positive integer as No.ofElements1/limit Parameter. Here at first, a string variable called “$strab” is created with the string value ‘car1, bus1, motorbike1, cycle1’. Then print_r() function is used along with the explode() function along with the three parameters. Here “,” is the Separator1 parameter, “$strab” is the original string element which is nothing but OriginalString1 parameter, “2” is the No.ofElements1/limit Parameter. According to the parameters description, if the positive integer value is passed then n-1 array indexes values will splitted and stored and for N-1 index value, remaining whole string will be printed.

Syntax:

<?php
$strab = 'car1, bus1, motorbike1, cycle1';
print_r(explode(',',$strab,2));
?>
Copy after login

Output:

PHP explode()

Example #4

This is the program of implementing the string splitting function by using different type of integer values for No.ofElements1/limit parameter. So that one can know what actually happens for different parameters which are acting inside of the explode() function. For the first explode() function, the whole original string is considered as only one array element. Then value “4” is used for the second explode() function. For this, n-1=3 array indexes string values will be printed but for n-1 array index the whole remaining string will be printed. Then for the third explode() function, negative integer value(-N) is used. So at N array index values the string will be trimmed and will be printed starting from the 0 index value.

Syntax:

<?php
$Original_str1 = "Hello, IamPavan Kumar Sake-Write, BusinessMan, PhysicsNerd.";
print_r (explode (" ",$Original_str1, 0));
print "\n";
print_r (explode (" ",$Original_str1, 4));
print "\n";
print_r (explode (" ",$Original_str1, -3));
print "\n";
?>
Copy after login

Output:

PHP explode()

The above is the detailed content of PHP explode(). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles