How To Split String In PHP Using Delimiter - explode() Function

The explode() function in PHP is used to split a string into multiple strings based on the specified delimiter. It is similar to the split function in Perl.

The syntax of the explode function is:

explode('delimiter', 'string', [limit])

The delimiter is the string (can be one or more characters) used to split string. [limit] is an optional parameter and has the following values:

  • > 0: Returns an array with a maximum of limit elements.
  • < 0: Returns an array except for the last-limit elements.
  • 0: Returns an array with 1 element.

Example 1

print_r (explode (',','one,two,three'));

Result:

Array ( [0] => one [1] => two [2] => three )

Example 2

$var = 'Learn PHP'; 
print_r (explode (' ',$var));

Result:

Array ( [0] => Learn [1] => PHP )

Example 3

print_r (explode (',','one,two,three',2));

Result:

Array ( [0] => one [1] => two,three )

Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive