PHP explode() Function | What is explode() in PHP?
Summary: In this article, we demonstrate and explore PHP explode() function, and what is explode() in PHP and how to use it? Let’s understand in detail this PHP function with an example.
What is PHP explode() Function?
The explode() function is a built-in function in PHP that is used to split a string into different strings or “Split a string by a specified string into pieces i.e. it breaks a string into an array”. The explode() function in PHP splits a string based on a string delimiter, i.e. this function splits the string while the delimiter character occurs. This common built-in function returns an array containing the strings formed by splitting the original string.
This function is binary-safe.
Syntax:
Parameter Value:
The PHP explode function accepts only three parameters of which two are mandatory and one is optional. All three parameters are Mentioned below:-
Parameter Value | Description |
---|---|
separator: | This character specifies the critical points or points at which the string will split, i.e. whenever this character is found in the string it symbolizes the end of one element of the array and the start of another. |
OriginalString: | The input string that is to be split into the array. |
NoOfElements: | This is an optional parameter. It’s utilized to specify the number of elements of the array. This parameter may be any whole number ( positive, negative, or zero).
|
Return Type: The return type of PHP explode() function is array of strings.
PHP explode() Function Example
Let’s see an example, you have a string such as given below:-
1 |
$mystr="W E L C O M E"; |
now you wish to make every name as an element of an array and access it individually so what you do:
1 |
$myarr = explode(",", $mystr); |
Means that we’ve made pieces of string $text
based on separator ','
and put the resulting array in variable $myarr
So I used print_r ($myarr), and therefore the results are the following:
1 2 3 4 5 6 7 8 9 |
Array( [0] => W [1] => E [2] => L [3] => C [4] => O [5] => M [6] => E ) |
so, means that which is equal to:
1 |
$myarr = Array ("W","E","L","C","O","M","E"); |
Program 1:
1 2 3 4 5 6 7 8 9 10 |
<?php // original string $OriginalString = "Keep your friends close, but your enemies closer."; // Without optional parameter NoOfElements print_r(explode(" ",$OriginalString)); // with positive NoOfElements print_r(explode(" ",$OriginalString,5)); // with negative NoOfElements print_r(explode(" ",$OriginalString,-2)); ?> |
[0] => Keep
[1] => your
[2] => friends
[3] => close,
[4] => but
[5] => your
[6] => enemies
[7] => closer.
)
Array (
[0] => Keep
[1] => your
[2] => friends
[3] => close,
[4] => but your enemies closer.
)
Array (
[0] => Keep
[1] => your
[2] => friends
[3] => close,
[4] => but
[5] => your
)
Program 2:
1 2 3 4 5 6 7 8 9 |
<?php $mystr = 'ram,shyam,krishna,radhe'; // zero limit print_r(explode(',',$mystr,0)); // positive limit print_r(explode(',',$mystr,2)); // negative limit print_r(explode(',',$mystr,-1)); ?> |
[0] => ram,shyam,krishna,radhe
)
Array (
[0] => ram
[1] => shyam,krishna,radhe
)
Array (
[0] => ram
[1] => shyam
[2] => krishna
)
Program 3: explode() Return Examples
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php /* A string that doesn't contain the delimiter will simply return a one-length array of the original string. */ $input1 = "welcome"; $input2 = "welcome,investor"; $input3 = ','; var_dump( explode( ',', $input1 ) ); var_dump( explode( ',', $input2 ) ); var_dump( explode( ',', $input3 ) ); ?> |
0 => string ‘welcome’ (length=7)
array (size=2)
0 => string ‘welcome’ (length=7)
1 => string ‘investor’ (length=7)
array (size=2)
0 => string ” (length=0)
1 => string ” (length=0)
Program 4: Limit Parameter Examples
1 2 3 4 5 6 7 |
<?php $mystr = 'ram|shyam|krishna|radhe'; // positive limit print_r(explode('|', $mystr, 2)); // negative limit (since PHP 5.1) print_r(explode('|', $mystr, -1)); ?> |
(
[0] => ram
[1] => shyam|krishna|radhe
)
Array
(
[0] => ram
[1] => shyam
[2] => krishna
)
Conclusion:
In this article, you have learned PHP explode() Function and explode() example.I hope you will enjoy it!. if have any query related program then contact me at info@tutorialscan.com I will try to resolve the problem. Stay healthy, stay safe! Happy coding! enjoy it!