PHP Explode Functions

PHP explode() is a built-in function that is used to split a string into a string array on boundaries formed by the separator. In this tutorial, we are going to discuss splitting a string into an array with explode() function and PHP loop.

explode(separator, string, limit = PHP_INT_MAX);
separatorRequiredThe boundary string. The “separator” parameter cannot be an empty string.
stringRequiredThe input string
limit OptionalIf the limit is positive, the returned array will contain a maximum of limit elements with the last element containing the rest of the string.
If the limit parameter is negative, all components except the last limit are returned.
If the limit parameter is zero, then this is treated as 1.
Returned ValueReturns an array of strings

This function is binary-safe.

$cars = "Ferrari Benz BMW Volvo Toyota";

var_dump(explode(" ", $cars));

Output

array (size=5)
  0 => string 'Ferrari' (length=7)
  1 => string 'Benz' (length=4)
  2 => string 'BMW' (length=3)
  3 => string 'Volvo' (length=5)
  4 => string 'Toyota' (length=6)

You can set the separator of any separator character.

$cars = "Ferrari,Benz,BMW,Volvo,Toyota";

var_dump(explode(",", $cars));

Output

array (size=5)
  0 => string 'Ferrari' (length=7)
  1 => string 'Benz' (length=4)
  2 => string 'BMW' (length=3)
  3 => string 'Volvo' (length=5)
  4 => string 'Toyota' (length=6)

PHP explode function with the limit parameter

$cars = "Ferrari,Benz,BMW,Volvo,Toyota";

var_dump(explode(',', $cars, 0));

Output

array (size=1)
  0 => string 'Ferrari,Benz,BMW,Volvo,Toyota' (length=29)

If you set the limit parameter to zero, the function will return the whole string as an array.

$cars = "Ferrari,Benz,BMW,Volvo,Toyota";

var_dump(explode(',', $cars, 2));

Output

array (size=2)
  0 => string 'Ferrari' (length=7)
  1 => string 'Benz,BMW,Volvo,Toyota' (length=21)

If you set the limit with a positive number, the function will return the exploded string into an array with the entered number of elements. If you enter the number that is more than the separated elements, elements will be the maximum number of the separated elements

$cars = "Ferrari,Benz,BMW,Volvo,Toyota";

var_dump(explode(',', $cars, -2));

Output

array (size=3)
  0 => string 'Ferrari' (length=7)
  1 => string 'Benz' (length=4)
  2 => string 'BMW' (length=3)

If you enter a negative number for the limit, the last items of the returned array will be ignored.

preg_split() function

PHP preg_split() function splits string by a regular expression.

preg_split (pattern , subject , limit = -1 , flags = 0 );
pattern RequiredDetermine a string as the pattern to search for.
subject RequiredThe input string.
limit OptionalIf specified, then only substrings up to limit are returned with the rest of the string being placed in the last substring. A limit of -1 or 0 means “no limit”.
flags Optionalflags can be any combination of the following flags (combined with the | bitwise operator):
PREG_SPLIT_NO_EMPTY
If this flag is set, only non-empty pieces will be returned by preg_split().
PREG_SPLIT_DELIM_CAPTURE
If this flag is set, parenthesized expression in the delimiter pattern will be captured and returned as well.
PREG_SPLIT_OFFSET_CAPTURE
If this flag is set, for every occurring match the appendant string offset will also be returned. Note that this changes the return value in an array where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1.
Returned ValueReturns an array containing substrings of subject split along boundaries matched by pattern, or false on failure.
$cars = preg_split("/[,]+/", "Ferrari,Benz,BMW,Volvo,Toyota");

var_dump($cars);

Output

array (size=5)
  0 => string 'Ferrari' (length=7)
  1 => string 'Benz' (length=4)
  2 => string 'BMW' (length=3)
  3 => string 'Volvo' (length=5)
  4 => string 'Toyota' (length=6)

PHP str_split() function

PHP str_split() function convert a string to an array

str_split ( string , length = 1 )
string RequiredThe input string.
length OptionalThe maximum length of the chunk.
Returned ValueIf the optional length parameter is specified, the returned array will be broken down into chunks with each being length in length, otherwise, each chunk will be one character in length.
$cars = "Ferrari,Benz,BMW,Volvo,Toyota";
var_dump(str_split($cars));

Output

array (size=29)
  0 => string 'F' (length=1)
  1 => string 'e' (length=1)
  2 => string 'r' (length=1)
  3 => string 'r' (length=1)
  4 => string 'a' (length=1)
  5 => string 'r' (length=1)
  6 => string 'i' (length=1)
  7 => string ',' (length=1)
  8 => string 'B' (length=1)
  9 => string 'e' (length=1)
  10 => string 'n' (length=1)
  11 => string 'z' (length=1)
  12 => string ',' (length=1)
  13 => string 'B' (length=1)
  14 => string 'M' (length=1)
  15 => string 'W' (length=1)
  16 => string ',' (length=1)
  17 => string 'V' (length=1)
  18 => string 'o' (length=1)
  19 => string 'l' (length=1)
  20 => string 'v' (length=1)
  21 => string 'o' (length=1)
  22 => string ',' (length=1)
  23 => string 'T' (length=1)
  24 => string 'o' (length=1)
  25 => string 'y' (length=1)
  26 => string 'o' (length=1)
  27 => string 't' (length=1)
  28 => string 'a' (length=1)

In this example the length is not defined then the function separated the string character by character.

$cars = "Ferrari,Benz,BMW,Volvo,Toyota";
var_dump(str_split($cars, 4));

Output

array (size=8)
  0 => string 'Ferr' (length=4)
  1 => string 'ari,' (length=4)
  2 => string 'Benz' (length=4)
  3 => string ',BMW' (length=4)
  4 => string ',Vol' (length=4)
  5 => string 'vo,T' (length=4)
  6 => string 'oyot' (length=4)
  7 => string 'a' (length=1)

You may like

Shopping Cart