PHP

PHP explode() Function

Introduction:

It is a built-in function in PHP, we use it for breaking string values into an array. Utilizing the explode() method we will make an array from a string. In simple words The explode() function breaks a string into an array, this function is binary-safe it’s a property basically, which means it can process any string.

Syntax:

explode(separator,string,limit);
  • In the above syntax, the explode() function is using a separator that will break the $string,
  • $string will declare or give by the user.
  • $limit is indicating how many values the method will return to the outcome array.

Parameters:

  1. The separator is used to decide where the string should break first.
  2. The second is String, that specific string which you want to break.
  3. the Last one is Limit, it’s optional.

Positive: If the value of the parameter is positive, it means this number of elements contained by the array after splitting the string according to the delimiter, assuming that the quantity of elements is more noteworthy than the given value, the main n-1elements stay all things considered. The last element contains the leftover string.

Negative: If the parameter value is negative, then the keep going n elements of the array will be eliminated, and the excess piece of the string will be returned as a substring in the array. If we pass zero the element will contain just those elements which would the whole string Return value returns an array of strings made by parting the string boundary on limits shaped by the separator. Assuming that separator is an unfilled string (” “), explode() will show a Value Error.

Now let’s do practice, this will be more clear to you.

Explode() Function

Example:

$str="E X P L O D E";

$res = explode(" ", $str);

print_r($res);

We have a string and we need to make each letter a component of an array and access it independently.

Output:

Array ( [0] => E [1] => X [2] => P [3] => L [4] => O [5] => D [6] => E )

Explode() function using limit

Example:

$str = 'apple|orange|mango|grapes';

$res = explode("|", $str, 3);

print_r($res);

This limit means that we have given a specific number after that it will break into an array.

Output:

Array ( [0] => apple [1] => orange [2] => mango|grapes )

Explode() function using negative(-) limit

Example:

$str = 'php,laravel_php,javascript,bootstrap';

$res = explode(',', $str, -1);

print_r($res);

This negative limit removed the value of that index number from an array.

Output:

Array ( [0] => php [1] => laravel_php [2] => javascript )

Explode() Function Empty String

Example:

$str = "";

$res = explode(",",$str);

print_r($res);

We passed an empty string in $str

Output:

Array ( [0] => ) // or sometimes nothing

In the event that you split the unfilled string, you get back the one-component array with 0 as the key and the vacant string for the value.

Difference Between Php Explode() & Php Split()

The fundamental contrast between the explode() and split() methods is parting an enormous string. Both methods are utilized to part a string. Nonetheless, the split() work is utilized to part a string utilizing a normal articulation, while the explode() function is utilized to part a string utilizing another string.

Conclusion:

In this tutorial we learned about PHP explode() function, we learn what is PHP explode() function and why we use it. We learn about its syntax and parameters and give a hand on its practical examples. I hope You will have proper knowledge about the PHP explode() function and you will be able to solve complex programming problems for more PHP tutorials.

Suggested Articles:

Leave a Comment