Finally got this working. Tried with a bunch of different test addresses, seems to work. If you entered "123921 kdjslf skdsls", this would sort it just fine, but the program as a whole would fail, hard.
<?php
$address = $_POST['address'];
$parts_array = explode(" ", $address);
if ($parts_array[0] > 0) { // the first piece of the address is a number
$number = $parts_array[0];
//echo "Number is $number <br>";
} else { // user has provided an address without a number
echo "This does not seem to be a valid address";
exit;
}
$fixes = array("NW", "NE", "SW", "SE", "N", "S", "E", "W");
$types = array("ACRD", "ALY", "AVE", "BLVD", "BR", "CIR", "CRST", "CT", "DR", "FRWY", "HWY",
"KY", "LN", "LOOP", "PKWY", "PL", "PT", "RD", "ST", "TER", "TRL", "WALK", "WAY");
for ($i=1; $i < count($parts_array); $i++) {
$parts_array[$i] = strtoupper($parts_array[$i]);
if (in_array($parts_array[$i], $fixes)) {
if ($i == 1) {
$prefix = $parts_array[$i];
} else {
$suffix = $parts_array[$i];
}
} elseif (in_array($parts_array[$i], $types)) {
$type = $parts_array[$i];
} else {
if (isset($name)) {
$name = $name . " " . $parts_array[$i];
} else {
$name = $parts_array[$i];
}
}
}
?>