<code><?php
function uslocation($string)
{
// Fill it with states
$states = array('D.C.', 'D.C', 'DC', 'TX', 'CA', 'ST');
// Extract state
$state = '';
foreach($states as $st)
{
$statepos = strpos(' '.$string, $st);
if($statepos > 0)
{
$state = substr($string, $statepos-1, strlen($st));
$string = substr_replace($string, '', $statepos-1, strlen($st));
}
}
if(preg_match('/([\d\-]+)/', $string, $zipcode))
{
$zipcode = $zipcode[1];
$string = str_replace($zipcode, '', $string);
}
else
{
$zipcode = '';
}
return array(
'city' => trim(str_replace(',', '', $string)),
'state' => $state,
'zipcode' => $zipcode,
);
}
// Some tests
$check = array(
'Washington D.C.',
'City Name TX',
'City Name, TX',
'City Name, ST, 0000',
'NY 7445',
'TX 23423',
);
echo '<pre>';
foreach($check as $chk)
{
echo $chk . ": \n";
print_r(uslocation($chk));
echo "\n";
}
echo '
';?>