PHP explode: Main Tips
- PHP explode function is used to get an array of strings from a single string.
explode()in PHP is completely binary-safe.
- It was introduced in PHP4 with the limit parameter, though negative limits weren't allowed until PHP 5.1.
Syntax: Parameters Explained
The correct PHP explode function syntax is displayed below:
Let's figure out what each parameter represents when using PHP explode function:
|separator||Needed. Defines the place where should PHP explode string. Cannot be an empty string!|
|string||Needed. Specifies the exact string to split.|
|limit||Optional. Defines the number of elements in an array to return after:|
|> 0 - Returns the array including a max of limit element(s)|
|< 0 - Returns the array except for the last -limit elements()|
|0 - Returns an array with one element|
Usage: Code Examples
Look at the example below. It shows different outcomes that can be achieved using limit parameter with PHP explode function:
'first,second,third,fourth'; // limit - zero print_r(explode(',', $string, 0)); // limit - positive print_r(explode(',', $string, 2)); // limit - negative print_r(explode(',', $string, -1));$string =
In this next example, comma is used as a separator. See how that turns out:
"This is example, with, commas!"; print_r (explode(",", $string));$string =
Latest Coupon Found:
TOP-RATED CODING COURSES
Learn the skills of tomorrow
The best time to learn programming is now - follow this link to access 100+ coding courses and enjoy learning at a very low cost!