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:
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:-
explode(String $delimiter, String $string, int $limit );
Check below table for the description of each parameter of the explode function of PHP.
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.
$string = "Tutorialdeep learning PHP explode function.";
$Sarray = explode("",$string);
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.
$Languagearray = explode(", ",$language);
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.
$count = "200, 500, 101, 209, 767, 485, 987";
$countarray = explode(", ",$count);
first = <?php echo $countarray."<br>"; ?>
second = <?php echo $countarray."<br>"; ?>
third = <?php echo $countarray."<br>"; ?>
forth = <?php echo $countarray."<br>"; ?>
fifth = <?php echo $countarray."<br>"; ?>
sixth = <?php echo $countarray."<br>"; ?>
sixth = <?php echo $countarray; ?>
second = 500
third = 101
forth = 209
fifth = 767
sixth = 485
sixth = 987
You must also read:-