PHP explode method to split a string with 3 examples

Purpose of explode method

The explode method, as the function name suggests, is used to split a string to the given number of substrings in PHP.

It returns an array of broken substrings that you may assign to an array variable.

You may find the detail of the explode method in the last part of this tutorial, first, have a look a the syntax and live examples of using the explode PHP function.

 

Syntax of using the PHP explode

This is how you can use the explode method of PHP:

An explode example to split phone numbers

In the following example, we have declared a string variable and assigned it a phone number in this format:

001-234-567678

After that, the explode method is used to split a string by using the hyphen (dash) delimiter. The returned array is assigned to an array.

Finally, we used a foreach loop to display the array element values that are split substrings of the phone number string. As there is no limit parameter, the whole phone number will be broken in three substrings. See the demo and code by clicking the following image or link:

PHP explode

See online demo and code

An example with space delimiter

In this example, we used the space delimiter. An example string is created as below:

This is explode tutorial that enables string split in PHP

Again, we did not use the limit parameter in that example. After creating the string, the PHP explode method is used to split that string. A foreach loop to display the returned array elements is used.

Finally, we used the count function to display the total number of substrings or elements in that array.

See example and output by the image or link below:

PHP explode foreach

See online demo and code

An example with the limit parameter

This is the same example as above except we used the limit parameter to specify the number of splits in the string. As such the string we used is:

“This is explode tutorial that enables string split in PHP”

As it returned total ten substrings while space delimiter was used. This time, we specified 5 splits by using the limit parameter. See the example and output online:

PHP explode limit

See online demo and code

You can see the demo page, only five substrings are displayed while the last substring is the remaining substring of the given string. The total count of the array element is also 5 rather 10 as in the above example.

Why we use explode method

The explode method is also searched as “PHP split” or split a string, which is used to split the given strings. For example, your web form is taking the phone number with country and area code in this format:

001-234-567678

While you need to separate the country and area code that are separated by dashes.

In that case, you can use the explode PHP method to break that phone number by using dash or hyphen as a delimiter, after taking this as input in string format.

The explode method will break this into following array elements since explode returns an array of broken string or substrings.

The First item = 001

The Second item = 234

The Third item = 567678

How to use explode function of PHP

As shown in the first part of this guide, you may specify three parameters in PHP explode function. Out of those, the last is an optional parameter i.e. specifying the number of splits or limiting the string pieces. See following part for description of each parameter:

 

  • The $array_name is an array; as such explode method returns an array of split strings.
  • The $delimiter specifies the boundary string. Like in the above example, we used “–“ as a delimiter to break the phone numbers. Similarly, you can use space, commas or any other words as a $delimiter.
  • The next parameter is the string that you want to split by PHP explode method. This can be a string variable or a sentence etc.
  • The last parameter specified is the limit or number of breaks in the given string. This is an optional parameter.

If a positive value is given in the limit parameter, the given string will be broken by that number. In that case, the last piece of substring will contain the rest of the string.

You may also use a negative value. In that case the, all substrings except the last one are returned.

As the explode function returns an array of the substrings, you can assign it to an array. After using the explode method, you can use the foreach loop to go through the array elements, as shown in the above examples.

Note: You can also use the str_split method to convert a string to an array.