PHP explode Function
PHP Explode function mainly used for breaking a string into a PHP array. Exact syntax of PHP explode() function is given,
explode(separator,string,limit)
Parameter | |
---|---|
separator | Required. Specifies where to break the string |
String | Required. The string to split in parts |
limit | Possible values: |
Greater than 0: – Returns an array with a maximum of limit element(s) | |
Less than 0: – Returns an array except for the last -limit elements()
0: – Returns an array with one element |
|
Changelog | The limit parameter was added in PHP 4.0.1, and support for negative limits were added in PHP 5.1.0 |
Return Value | Returns an array of strings |
PHP Version | 4+ |
Table Reference: w3schools.com
Example of PHP explode Function
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $str = 'php,coder,tech,explode'; // zero limit print_r(explode(',',$str,0)); // positive limit print_r(explode(',',$str,2)); // negative limit print_r(explode(',',$str,-1)); ?> |
Output:
1 2 3 4 5 |
Array ( [0] => php,coder,tech,explode ) Array ( [0] => php [1] => coder,tech,explode ) Array ( [0] => php [1] => coder [2] => tech ) |
Note: The “separator” parameter cannot be an empty string.
Note: This function is binary-safe.
Also Check: jQuery autocomplete ajax with Text and Image
Happy Coding..!
Was this article helpful?
YesNo
2 Replies to “A Beginner’s Guide to PHP Explode Function | explode()”