- is_numeric: documentation ( source)
- array_reverse: documentation ( source)
- array_slice: documentation ( source)
- implode: documentation ( source)
- explode: documentation ( source)
<?php
function str_to_address($context) {
$array = explode(" ", $context);
$array_reversed = array_reverse($array);
$numKey = "";
$zipKey = "";
foreach($array_reversed as $k=>$str) {
if($zipKey) { continue; }
if(strlen($str)===5 && is_numeric($str)) {
$zipKey = $k;
}
}
$array_reversed = array_slice($array_reversed, $zipKey);
$array = array_reverse($array_reversed);
foreach($array as $k=>$str) {
if($numKey) { continue; }
if(strlen($str)>1 && strlen($str)<6 && is_numeric($str)) {
$numKey = $k;
}
}
$array = array_slice($array, $numKey);
$string = implode(' ', $array);
return $string;
}
echo str_to_address("Sally sells seashells at 819 Timber Glen Ln. Ballwin and loves it.");
?>