What is PHP explode function
An explode is the process of converting the characters of the string into smaller pieces. A string is the sequence of characters with some size limit.
The string which you want to explode can be a sentence which you can break into some parts which can be smaller or some combination of characters.
Suppose there are some string of characters like “This is the great world” and after you explode it into small pieces, the final output after the conversion is a sequence given below:
1. This
2. is
3. the
4. great
5. world
The explode removes the delimiter which is the space or any other among the words.
Why we use PHP explode() function
We can use PHP explode function to convert a string of characters into an array. A string of characters can be a small or large sentence. PHP explode is useful when you want to catch and display each word of a sentence separately. The syntax of the PHP explode function is given below:-
Syntax
1 2 3 |
<?php explode(String $delimiter, String $string, int $limit ); ?> |
Check below table for the description of each parameter of the explode function of PHP.
Parameter Name | Description |
---|---|
$delimiter |
|
$string |
|
$limit |
|
Example of PHP explode() function to break a string into an array
Example 1: Let us take a simple example of a sentence which you want to convert into a string using the PHP explode function. The string contains about five words which on breaking give out five parts in an array. We can use PHP print function to print the resulted arrays.
The delimiter for this sentence is a single space which you can remove using the first parameter of the explode function. Use a single space in the delimiter parameter and the string in the second parameter.
1 2 3 4 5 |
<?php $string = "Tutorialdeep learning PHP explode function."; $Sarray = explode("",$string); print_r($Sarray); ?> |
Output
Example2: There are seven words in the below given string with the delimiter comma and a space. The example removes the delimiter comma and space and return out the seven parts in an array.
1 2 3 4 5 |
<?php $language = "PHP, Java, HTML, CSS, Javascript, jQuery, Ajax"; $Languagearray = explode(", ",$language); print_r($Languagearray); ?> |
Output
Example 3 : You can also print each array part individually using the PHP echo statement. Here, in this example, we first convert the given $count variable value into an array and then print each array one by one. Let’s see how you can do this with the below given example.
1 2 3 4 5 6 7 8 9 10 11 |
<?php $count = "200, 500, 101, 209, 767, 485, 987"; $countarray = explode(", ",$count); ?> first = <?php echo $countarray[0]."<br>"; ?> second = <?php echo $countarray[1]."<br>"; ?> third = <?php echo $countarray[2]."<br>"; ?> forth = <?php echo $countarray[3]."<br>"; ?> fifth = <?php echo $countarray[4]."<br>"; ?> sixth = <?php echo $countarray[5]."<br>"; ?> sixth = <?php echo $countarray[6]; ?> |
Output
second = 500
third = 101
forth = 209
fifth = 767
sixth = 485
sixth = 987
Reference
You must also read:-
Thanks for this article, I was not aware of the third parameter which can be useful when I want to limit the number of elements