English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The explode() function is used to split one string into another string using a delimiter and return an array of strings.
array explode ( string $delimiter , string $string [, int $limit ] )
It is used to split strings by string
It returns an array of strings
Serial number | Parameters and descriptions |
---|---|
1 | delimiter (required) Boundary string |
2 | string (required) Input string. |
3 | limit (optional) If the limit parameter is set and is positive, the returned array contains up to limit elements, and the last element will contain the remaining part of the string. |
Try the following example, explode splits the string with spaces and returns an array:
<?php $str = "w"3codebox simply easy learning."; print_r(explode(" ", $str)); ?>Test and see‹/›
Output result
Array ( [0]=>w3codebox [1]]=>simply [2]]=>easy [3]]=>learning.)
The following example demonstrates using commas to separate characters, as well as an array of a single length of the original string without delimiters.
<?php /* An array of a single length of the original string without delimiters. */ $input1 = "hello"; $input2 = "hello,there,w"3codebox,com" print_r(explode(',', $input))1 ); print_r(explode(',', $input))2 ); ?>Test and see ‹/›
Output result
Array ( [0]=>hello ) Array ( [0]=>hello [1]]=>there [2]]=>w3codebox [3]]=>com )
The following examples specify the limit parameter and return an array element instance:
<?php $str = 'one|two|three|four'; // Positive limit print_r(explode('|', $str, ',')) 2)); // Negative limit() in PHP 5.1 ) print_r(explode('|', $str, ',')) -1)); ?>Test and see ‹/›
Output result:
Array ( [0]=>one [1]]=>two|three|four ) Array ( [0]=>one [1]]=>two [2]]=>three )