What is PHP explode, and how to use explode()

Sometimes we require to convert the string into an array. PHP provides the feature of converting strings into an array. We can use explode() function for converting the string into an array.

What is explode() function?

The explode() is the string function in PHP which is used for converting the string into an array.

How to use explode() function?

we can use explode function in PHP as given below.

Syntax:

explode(separator, string, limit)

Explanation:

1) separator

It indicates where you want to break the string.
It is the required property of explode function.

2) string

It indicates the string which you want to separate.
It is the required property of explode function.

3) limit

It is an optional field of the explode function.

Sometimes we want to break the string into the fixed size of the array at that time limit field is used.
If we set this field to 3, then the explode, the function will break the string into an array of size 3.

Example 1

$language = "HTML CSS JAVASCRIPT PHP PYTHON JQUERY";
$arr = explode(" ",$language);

Explanation:

In the above example, we take a string into variable language then we use explode function for converting the string into an array and store the array into an array named as arr.

Here we have used ” “( space ) as a separate of the string.
We use $language as a string of the explode function.

Note: We can use different symbols as separate. Example: space, comma, dot, and many more.

Example 2

Suppose we have a string “Hello. I am errorsea best teaching platform for students.” and we want to break the string into an array.

<html>
<body>

<?php
$message = 'Hello. I am errorsea best teaching platform for students.';
echo "<pre>";
print_r(explode(" ",$message));
echo "</pre>";

?>

</body>
</html>

Output:

Array
(
[0] => Hello.
[1] => I
[2] => am
[3] => errorsea
[4] => best
[5] => teaching
[6] => platform
[7] => for
[8] => students.
)

Explanation:

  • First we put the string into a variable message.
  • Then we use explode function, and we pass space as a separator of string.
  • We use print_r function to print the resulted array.
  • Here we use the <pre> tag to show systematic output.

Example 3

If we use a full stop( . ) as a breakpoint, then we will get the output as below:

Array
(
[0] => Hello
[1] => I am errorsea best teaching platform for students
[2] =>
)

Explanation:

Here full stop breaks the string into two filled components and one empty component of the array.
The last empty component is for white space after the last full stop.

Example 4

If we change the string to “Hello. I am errorsea best teaching platform for students” and use full stop as a breakpoint then it generates the output as follows:

Array
(
[0] => Hello
[1] => I am errorsea best teaching platform for students
)

Note:

The only difference with example 3 is that it does not create an empty element of an array because there is no full stop at the end of the string.

Various forms of the limit field with explode function:

Example 5

Suppose we want to break a string “Hello. I am errorsea best teaching platform for students.” into an array of size 3.

<html>
<body>

<?php
$message = 'Hello. I am errorsea best teaching platform for students.';
echo "<pre>";
print_r(explode(" ",$message,3));
echo "</pre>";

?>

</body>
</html>

Output:

Array
(
[0] => Hello.
[1] => I
[2] => am errorsea best teaching platform for students.
)

Example 6

If we use 0 as a limit, then it will give us the output as follows:

Output:

Array
(
[0] => Hello. I am errorsea best teaching platform for students.
)

Here we get the whole string as a single object of an array because we set the limit to 0.

Example 6

If we use negative limit it stats removing the elements from the right side of the string:

For example, We have a string “Hello. I am errorsea best teaching platform for students.” and we want to remove the last two elements of the string then we use the code as follows:

<html>
<body>

<?php
$message = 'Hello. I am errorsea best teaching platform for students.';
echo "<pre>";
print_r(explode(" ",$message,-2));
echo "</pre>";

?>

</body>
</html>

Output:

Array
(
[0] => Hello.
[1] => I
[2] => am
[3] => errorsea
[4] => best
[5] => teaching
[6] => platform
)

Explanation:

The negative limit starts counting the string position from right to left.
Here we want to remove the last two elements, and then we set the limit field of the explode function to -2.

Conclusion:

These are the different forms of explode() function in PHP. We can use them according to the requirement of the program. It is very useful when we want to use a specific word from the string.

Also Read: PHP String Functions

Leave a Reply

Your email address will not be published. Required fields are marked *

More in PHP
php operators
List of PHP Operators

PHP provides different types of operations similar to other programming languages. They are divided into the following categories: Arithmetic operatorsLogical...

Close