Whenever you desire to change a string into small parts then explode is the best used function for you. In case, you want to change a sentence into small particles, then use the command.
For instance; your required sentence is “Hi John, I am just coming here”.
This sentence will be like this:
1. Hi
2. John,
3. I
4. am
5. just
6. coming
7. here.
Now, we can not find the sentence but we can see the scattered particles of it. Now, you have an idea of the work of this function. Here is an example which will make it clearer.
The function of explode:
The function requires a delimiter for make the string to scatter into small pieces. The function explode gives back the real words in the form of an array. All the data in the array is gathered according to the sentence order. We can give the example of a mobile number. We have divided it by using hyphen.
$phoneChunks = explode("-", $rawPhoneNumber);
echo "Real Phone Number = $rawPhoneNumber <br />";
echo "1st part = $phoneChunks[0]<br />";
echo "2nd part = $phoneChunks[1]<br />";
echo "3rd part = $phoneChunks[2]";
Real Phone Number = 0323-228-7888
1st part = 0323
2nd part = 228
3rd part = 7888
How to set a limit in explode function?
In case, you would like to get the number of division done by this explode function then you can think up of one more argument. This argument can make the divided particles into a pattern. It counts the parts. When the parts become equal to the real number then it stops dong its function.
$wordChunks = explode(" ", $someWords);
for($i = 0; $i < count($wordChunks); $i++){
echo "Piece $i = $wordChunks[$i] <br />";
}
$wordChunksLimited = explode(" ", $someWords, 4);
for($i = 0; $i < count($wordChunksLimited); $i++){
echo "Limited Piece $i = $wordChunksLimited[$i] <br />";
}
Piece 0 = I
Piece 1 = will
Piece 2 = give
Piece 3 = you
Piece 4 = possible
Piece 5 = help.
Limited Piece 0 = I
Limited Piece 1 = will
Limited Piece 2 = give
Limited Piece 3 = you possible help.
Here, we have divided the sentences into pieces. The limit of characters is four. That is why the last piece has three words in it.
Leave a Reply