Search code examples
phplimitexplode

How to limit the elements created by explode()


If i have a string like

"1234 Name Extra"

And i do

list($postcode, $city) = explode(" ", $string);

It turns out that $postcode is "1234" and $city is "Name". I would like $city to be "Name Extra"

How can i do this?


Solution

  • Use the third parameter ($limit) of explode. The resulting array will have $limit Elements.

    list($postcode, $city) = explode(" ", $string, 2);