PHP Explode() Function

PHP Explode The PHP explode( ) is used to break a string into an array. The PHP explode( ) allow us to break a string into a smaller text with each break is represented the same symbol. This symbol is known as a delimiter. Using the PHP explode( ), we can create an array from string. Syntax:

explode (separator, string, limit)
  • Separator: It is required. It specifies where to break the string.
  • String: It is required. The string to split.
  • Limit: It is optional. It specifies the number of an array element to return.
Possible Values: Greater than 0: It returns an array with a maximum of elements. Less than 0: It returns an array except for the last. 0: It returns an array with one element. Note: We must ensure that the delimiter argument before the string argument. Example1:
<?php
$str = "PHP explode.";
print_r (explode(" ",$str));
?>
Output:
Array ( [0] => PHP [1] => explode )
In the above example, we have $str="PHP explode." We made each name as an element of an array and accessed it individually, i.e., (explode(" " ,$str)); Resulting Array Variable:
Array ( [0] => PHP [1] => explode )
Example2(Limit Parameter):
<?php
$str = ‘PHP explode.';
print_r (explode(' ',$str,0));
print"<br>";
print_r (explode(' ',$str,2));
print"<br>";
print_r (explode(' ',$str,-1));
?>
Output:
Array ( [0] => PHP explode. ) 
Array ( [0] => PHP [1] => explode. ) 
Array ( [0] => PHP )



ADVERTISEMENT
ADVERTISEMENT