PHP is a general-purpose programming language suited for dynamic web development. It offers a wide variety of built in functions. One such function that we are going to see today in this tutorial is explode in PHP.
What Is Explode in PHP
A built-in function in PHP that splits a string into different strings is known as explode(). The splitting of the string is based on a string delimiter, that is, explode in PHP function splits the string wherever the delimiter element occurs. This function is binary-safe and returns an array of strings as a result of splitting the original string.
We will look at the use and different parameters of the explode method in the later parts of the tutorial, but first, let us look at the syntax and an example of the explode in PHP function.
Syntax:
array explode(delimeter, String, NumOfElements)
Example:
Code-
<!DOCTYPE html>
<html>
<body>
<?php
$string = "Let's look at the explode function.";
print_r (explode(" ",$string));
?>
</body>
</html>
In this example, a space character splits the string, i.e. it is used as the delimiter/ separator.
Output:
Use of Explode Function
The primary function of explode in PHP is to break a string into an array. The return type of the function is an array of strings. This method is also referred to as ‘split a string’ or ‘PHP split’ as it splits a string. For example:
You are taking user input (phone number with area code and country code) in the format: 001-125-673837
To process the input, you need to separate the area code and country code, distinguished by dash (-). In such cases, we use explode in PHP method as it breaks the phone number by using a character as the delimiter. The only necessary condition is that we take the input as a string. Here is the code:
<!DOCTYPE html>
<html>
<body>
<?php
$num = "001-125-673837";
$arr = explode("-",$num);
//foreach loop is used to display the returned array
foreach($arr as $i){
echo $i . "<br />";
}
?>
</body>
</html>
Output:
Explode in PHP method breaks into array elements and returns an array of substrings or broken strings. Here’s another example:
<!DOCTYPE html>
<html>
<body>
<?php
$ string = "Let's look at the explode in PHP function.";
print_r(explode(" ",$String));
print_r("___________________");
print_r(explode(" ",$String,3));
print_r("___________________");
print_r(explode(" ",$String,-1));
?>
</body>
</html>
Output:
In the above example, there are three expressions. In the first expression, the third parameter has not been specified, and the new array has been created with the help of the space character. However, in the second and third expression, we have passed the third parameters, and it clubs the last three substrings and removes the last substring as per the parameter value.
Explode() Function Parameter Values and their Use
The explode in PHP function accepts three parameters. One parameter is optional, and the other two are compulsory. These three parameters are as follows:
- delimiter: This character is the separator which specifies the point at which the string will be split. Whenever this element is found in the string, it marks the end of one array and the start of another. The delimiter parameter cannot be empty.
- String: This is the original string that is required to be split into substrings.
- NumOfElements: This is the only optional parameter. It specifies the number of elements of the array. It can be either positive, negative or zero.
- Positive: When the parameter has a positive value, this means that array contains this number of elements. After separating the string as per the delimiter, if the number of elements is greater than the given value, the first n-1 elements remain as it is. The last element comprises the remaining string.
- Negative: If the parameter value is negative, then the last n elements of the array will be removed, and the remaining part of the string will be returned as a substring in the array.
- Zero: When the parameter is zero, the returned array has only one element, that is, the whole string.
By default, if the numOfElements is not specified, the array returned consists of all substrings separated by the delimiter.
Example:
The numOfElements is also called the limit parameter and here’s how it is used:
<!DOCTYPE html>
<html>
<body>
<?php
$string = 'Use,of,limit,paramter';
print_r(explode(',',$string,0));
print_r("____________ ");
print_r(explode(',',$string,2));
print_r("____________");
print_r(explode(',',$string,-1));
?>
</body>
</html>
Output:
Note: explode in PHP function is supported by PHP version- 4+, and the limit parameters were added in the later versions.
Are you a web developer or interested in building a website? Enroll for the Full Stack Web Developer - MEAN Stack Master's Program. Explore the course preview!
Conclusion
We have reached the end of this explode in PHP tutorial. We saw how PHP offers explode() function to split the string as per the separator. It is different from other tokenization methods present as the separated strings are returned as an array. In case you wish to master full stack development, or make a solid career in web development, Simplilearn’s PGP in Full Stack Development should be your next step! Start learning now.