Home >>PHP String Functions >PHP explode() Function
The PHP explode() function is used to divide or split any given input string in to different strings.
This explode() function is an in-built function of PHP. This explode() function returns an array as a result. The resultant array contains all the strings created by splitting the original given input string.
PHP explode() function splits the input string based on the separator, which means the function will split the string whenever the separator occurs in the string. The $limit is an optional parameter which defines the maximum index of the resultant array.
Syntax :
explode($separator,$string,$limit);
Here is an example of explode() function in PHP:
<html> <body> <?php $str = "Laravel is a compelling model view controller (MVC) architectural pattern PHP framework."; print_r (explode(" ",$str)); ?> </body> </html>
Here is an example of explode() function in PHP using separator(,) :
<html> <body> <?php $str = "Laravel is a compelling model view controller (MVC) architectural pattern PHP framework."; print_r (explode(" ",$str)); ?> </body> </html>
Here is an example of explode() function in PHP using different limits :
<html> <body> <?php $str = 'PHP,Python,C++,HTML,Laravel,C#'; print "Zero Limit:<br>"; print_r(explode(',',$str,0)); print "<br><br>"; print "One Limit:<br>"; print_r(explode(',',$str,1)); print "<br><br>"; print "Zero Limit:<br>"; print_r(explode(',',$str,4)); print "<br><br>"; print "Negative Limit:<br>"; print_r(explode(',',$str,-1)); print "<br><br>"; print "Negative Limit:<br>"; print_r(explode(',',$str,-3)); ?> </body> </html>