I have a set of addresses stored as individual strings. Here are possible values of those strings:
"Staples Center, 555 Test Drive, Los Angeles, CA 98112"
"555 Test Drive, Los Angeles, CA 98112"
"Los Angeles, CA"
"Los Angeles, CA 98112"
"Los Angeles"
The dates will always follow this format, where it could contain a venue name, street address, city, state, and zip
I would like to dynamically extract the city and state only from these strings:
$addresses_array = array(
"Staples Center, 555 Test Drive, Los Angeles, CA 98112",
"555 Test Drive, Los Angeles, CA 98112",
"Los Angeles, CA",
"Los Angeles, CA 98112",
"Los Angeles"
);
foreach ($addresses_array as $address) {
if (strpos($address, ',') !== false) {
// extract the city and state
}
}
Given the pattern of the address values, is there something I can do to extract the city and state based on the commas?
What I can see is that the city will always be the last comma in the string..