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?
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?
Use the third parameter ($limit) of explode. The resulting array will have $limit Elements.
list($postcode, $city) = explode(" ", $string, 2);
list($postcode, $city) = explode(" ", $string, 2);
If you look at the PHP docs, you'll see that explode()
has an optional third parameter that specifies how many times you want to explode.
From the manual:
array explode ( string $delimiter , string $string [, int $limit ] )
so you use the optional limit argument, in your case 2, I believe
© 2022 - 2024 — McMap. All rights reserved.