How PHP explode Function Convert String Into an Array

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

Check below table for the description of each parameter of the explode function of PHP.

Parameter Name Description
$delimiter
  • This is the required field where you have to put the string which can be a separator.
  • space, dash, quotes are some example of the delimiter.
  • e.g: $string = “the,helpful,thing”. Here ,(comma) is the delimiter which you can remove using , in place of $delimiter.
$string
  • This is the required field where you have to put the string character.
  • A string can be a sentence or group of words which you can inside the explode function of PHP.
$limit
  • This is the optional field.
  • In limit field, you have to specify the number of array you want to break the string.
  • If you specify this field as zero, it will automatically be treated as 1.
  • If you specify -1 as the value of limit, it will not return the last word and convert others into array.

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.

Output

Array ( [0] => Tutorialdeep [1] => learning [2] => PHP [3] => explode [4] => function. )

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.

Output

Array ( [0] => PHP [1] => Java [2] => HTML [3] => CSS [4] => Javascript [5] => jQuery [6] => Ajax. )

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.

Output

first = 200
second = 500
third = 101
forth = 209
fifth = 767
sixth = 485
sixth = 987

Reference

PHP.net explode function.

You must also read:-

One reply on “How PHP explode Function Convert String Into an Array”

  1. 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

Comments are closed.