I have the following code, which takes a location and extracts the city and state/country out of it:
$address_input = "555 Test Drive, Johannesburg, South Africa";
if (strpos($address_input, ',') !== false) {
$arr = explode(", ", $address_input);
if(count($arr) >1){
$arr[count($arr)-1] = explode(" ", $arr[count($arr)-1])[0];
$address = implode(", ", array_slice($arr, -2));
}
}
The address outputs as:
"Johannesburg, South" instead of "Johannesburg, South Africa"..
How can I modify the code above to return the full last part of the address like in the example above? I see that implode catches all the pieces with a comma after it but how can I get the last piece of the address to contain the full state or country name? I don't know where to go from here.
Thank you!