PHP explode Function

PHP Tutorial > String Functions > 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. Earlier versions of PHP had its own version of the split function, but it was deprecated as of PHP 5.3.0 and removed from PHP altogether as of 6.0.0. Using the split function in PHP is strongly discouraged.

The syntax of the explode function is:

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

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 element(s).

< 0: Returns an array except for the last -limit element(s).

0: Returns an array with 1 element.

Let's take a look at the examples below:

Example 1

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


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

Example 2

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


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

Example 3

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


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

Next: PHP htmlentities Function

Copyright © 2023  All Rights Reserved.  Privacy Policy   About   Contact