Reputation: 147
I have an API call returning a field meetingAddress with the following format. Street " " City "," State ZipCode. The "" in this example is to show where the matching characters fall into the string.
I have fiddled around with substr and strpos but due to my limit experience cannot seem to get it to work. I am writing a function to take the address and return just the city and state.
$str needs to be populated with the MeetingAddress data
$from = "#xD;"; - this is always before the city
$to = ","; - this is after the city
echo getStringBetween($str,$from,$to);
function getStringBetween($str,$from,$to)
{
$sub = substr($str, strpos($str,$from)+strlen($from),strlen($str));
return substr($sub,0,strpos($sub,$to));
}
Here is an exact example of what is being returned.
<d:MeetingAddress>44045 Five Mile Rd
Plymouth, MI 48170-2555</d:MeetingAddress>
Here is a second example:
<d:MeetingAddress>PO Box 15526
Houston, TX 77220-5526</d:MeetingAddress>
Upvotes: 2
Views: 164
Reputation: 1584
$str = "<d:MeetingAddress>44045 Five Mile Rd
Plymouth, MI 48170-2555</d:MeetingAddress>";
preg_match('/
(.*),(.*) /', $str, $matches);
$matches[1]
is the city, $matches[2]
is the state
Upvotes: 1
Reputation: 5316
Simply use explode()
function:
$str = '<d:MeetingAddress>44045 Five Mile Rd
Plymouth, MI 48170-2555</d:MeetingAddress>';
$tmp = explode(';', $str);
$details = explode(',',$tmp[1]);
$details[1] = substr(trim($details[1]),0,2);
var_dump($details);
Output:
Array
(
[0] => Plymouth
[1] => MI
)
Upvotes: 0
Reputation: 642
You can do like below
$string = '44045 Five Mile Rd
Plymouth, MI 48170-2555';
list($address,$cityAndState) = explode('#xD;',$string);
list($city,$state) = explode(',',$cityAndState);
echo $address;
echo $city;
echo $state;
Upvotes: 0