Programming PHP How to convert string to array in PHP by PHP explode() function

How to convert string to array in PHP by PHP explode() function

How to convert string to array in PHP by PHP explode() function

To convert string to array in PHP, we do not need any complicated operations on string. Neither have we needed to process the string in a loop. PHP is a very feature-rich language and has a lot of built-in functions to facilitate easy to complex operations. We can convert string to array in PHP by using a simple function, that is, PHP explode() function. Let’s know about this PHP function and learn to create different array elements from a string in PHP.

What is explode() function in PHP

PHP explode() function is a very useful array function by PHP core. There is absolutely no need to create multiple loops or use complex solutions in order to derive an array from a string. PHP provides this built-in function in its core and has been implemented to achieve this.

This PHP explode() function has 3 parameters.

The first two are the mandatory ones. However, the third one is optional. In fact, I would say it is of not much use and I never had the requirement to use it.

The first parameter is the delimiter. The delimiter is the part of the string which this function will identify as a breakpoint. The string will be divided into parts on the basis of this string. If there is no occurrence of the given delimiter, string will be assigned to the first index of the array.

The second parameter is the string itself that will be divided into parts to form the array.

The third parameter is a way more complicated to understand for beginners and students and consists of the limit which is an integer value. The default value of the limit is null, and you can provide any integer as a value. The limit can be positive, negative or zero.

If the limit value given is zero, then the complete string will be assigned to the first index of the array formed. In case the limit is positive (say n), then the array will be created will be of n indexes. If the limit will be negative (say -n) then the array will have all the number of elements equal to elements formed-n. I will explain all the cases in example for better understanding.

Now, the question is, what do we need to create different array elements from a string? Well, it’s easy! We need the string and the elements that make it up.

Let’s take a look at the example below:

“Hi There I Am Techbriefers to welcome you here”

This is the string; the elements that we need and that must be inserted inside the array are the words. To explode this string into array, we need a delimiter that can be any character or a set of characters. However, to break words in this into array values, we need to give space as the delimiter. So, every element will be exploded on the basis of space in between. I am going to mention all the cases in the examples below. Do have a look.

Examples to show: how to convert string to array in PHP with PHP explode()

I am going to use the same string in all the examples below. And before printing arrays, I am printing a pre tag so that array should print in a visible format.

#1: Convert string to array with spaces

In this case, I am simply breaking the string into array with no limits and space as the delimiter.

The result of this explosion is:

#2: Using imperfect delimiter in explode()

This example is just to demonstrate the situation when we will use wrong or imperfect delimiter in explode() function.

This will break the string from the point where ‘Am’ is found, and provide the result below.

#3: String to array conversion in PHP with limit 1

I am using a limit in this case. Limit is the third parameter and is rarely used. Let me remind you that by default the limit is null and we can provide any positive or negative integer value to this parameter. Here I am giving a positive limit of 1. This will break the string into an array of length 1. This means all the characters will be assigned to the first index.

#4: PHP explode() function with positive limit parameter = 2

Likewise the previous one, here also I am using a positive limit. But this time I am using 2 as a limit. Let’s see how it results.

Output:

So, it converted the string to an array of length 2.

#5: String to array conversion in PHP with negative limit -1

Unlike the last two cases, the limit parameter, in this case, is a negative value, i.e., -1. This will break the string into an array of length 1. In this case, the explosion will occur normally like in example#1. However, focus on the last index which is missing. That means the string will be converted to an array with a length n characters less than the original one.

Result:

#6: Example of PHP explode() function with negative limit -2

Taking a similar example like the example#5 but with limit changed to -2. This will remove the last two indexes in the array formation.

Result of example#6

#7: Convert string to array in PHP with limit 0

In this case, I am applying a limit of 0. This will result in array of length 1. The array created will have all the characters assigned to the first index, which results in the assignment of complete string to the first index.

Result of example#7

There are some important factors that you need to consider while implementing the explode() function:

If the delimiter is or composed of an empty string then the function will return the value FALSE. Along with this, you will get a warning of empty delimiter used.

If you are using a delimiter which is not present in the given string, and the limit mentioned is negative. Then the returned value will be an empty string.

Below are the examples to show such cases.

#8: explode function with empty delimiter

#9: explode function with a wrong delimiter and negative limit -1

#10: Explode function with wrong delimiter and negative limit -4

Leave a Reply