What is explode in PHP?
The explode is a built-in function in PHP to convert a string to array by a split with the help of delimiter.The explode takes a string, separator, and limit for performing this action. The return type of this function is array.
Syntax:
<?php explode(separator,string,limit); ?>
Parameter:
separator(required): separator describes where it needs to break the string.
string(required): string which needs to break
limit(optional): specifies the number of elements in the array needs to return.
Possible values of limit:
a positive number(n): Returns array having a maximum of limit elements
a negative number(-n): Returns array having elements except last.
Zero: Returns array having only one element.
Examples:
<?php $inputString = 'john says good morning'; echo 'zero limit:'; echo '<pre>'; print_r(explode(' ',$inputString,0)); echo '</pre>'; echo 'positive number limit:'; echo '<pre>'; print_r(explode(' ',$inputString,2)); echo '</pre>'; echo 'positive number limit:'; echo '<pre>'; print_r(explode(' ',$inputString,6)); echo '</pre>'; echo 'negative number limit:'; echo '<pre>'; print_r(explode(' ',$inputString,-1)); echo '</pre>'; echo 'Without limit'; echo '<pre>'; print_r(explode(' ',$inputString)); echo '</pre>'; ?>
Output:
