English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP explode() Function Usage and Example

   PHP String Function Manual

   The explode() function is used to split one string into another string using a delimiter and return an array of strings.

Syntax

array explode ( string $delimiter , string $string [, int $limit ] )

Definition and usage

It is used to split strings by string

Return value

It returns an array of strings

Parameter

Serial numberParameters 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.
If the limit parameter is negative, it returns all elements except the last -all elements except the limit elements.
If limit is 0, it is treated as 1.

Online example

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.)

More examples

 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
)

PHP String Function Manual