explode()

The explode() function is used to break an array into a list of elements, based on a separator.

explode() uses the first argument as the separator, and the second as the string.

explode()``() has an alias called ``split().

explode() creates an empty string when the separator is at the beginning or the end of the strings. It also build empty strings when the separator are next to each other.

The separator has to be a static string. For dynamic separators, there is the preg_split() function.

explode() has a third parameter to limit the number of read elements. It is useful to avoid spending too much resources (time, memory..) working on the string, when a maximum number of expected elements is known, and the string is not.

<?php

print_r(explode('&', '&a=3&b') );
// ['', 'a=3', 'b']

?>

Documentation

See also Beginner’s Guide to PHP explode() Function (With Code Examples!).

Related : preg_split(), Separator