Stop Microsoft
Miscellaneous => Programming & Networking => Topic started by: worker201 on 5 May 2010, 22:49
-
I've been trying to write an address parsing routine in PHP. The user is prompted to input a street address. In my area, the address is of the form:
number prefix street type (example: 101 N 37th Ave)
or
number street type suffix (example: 101 37th St NW)
Of course, the user might simply put in 101 14th, but that's okay, I think I've covered that. What I'm not sure about is how to deal with multi-word streetnames. What if the address is 101 SW Happy Morning Bluebird Dr?
Here's the code I've got so far, which works great for single-word streetnames, and even deals with incomplete addresses. Am I going about this in the wrong way? Got any ideas how I can improve my algorithm?
The address parts will eventually be used to search a street database.
<?php
$address = $_POST['address'];
$parts_array = sscanf($address, "%d %s %s %s");
if ($parts_array[0] > 0) {
$number = $parts_array[0];
//echo "Number is $number <br>";
}
else {
echo "This does not seem to be a valid address";
exit;
}
$fixes = array("NW", "NE", "SW", "SE", "N", "S", "E", "W");
if (in_array($parts_array[1], $fixes)) {
$prefix = $parts_array[1];
//echo "Prefix is $prefix <br>";
$name = strtoupper($parts_array[2]);
//echo "Name is $name <br>";
if ($parts_array[3] != NULL) {
$type = strtoupper($parts_array[3]);
//echo "Type is $type <br>";
}
}
elseif (in_array($parts_array[3], $fixes)) {
$name = strtoupper($parts_array[1]);
//echo "Name is $name <br>";
$type = strtoupper($parts_array[2]);
//echo "Type is $type <br>";
$suffix = $parts_array[3];
//echo "Suffix is $suffix <br>";
}
else {
$street = strtoupper($parts_array[1]);
//echo "Street is $street <br>";
if ($parts_array[2] != NULL) {
$type = strtoupper($parts_array[2]);
//echo "Type is $type <br>";
}
}
?>
-
can you make it recognise every string that might be considered a "type" (like with your array for the prefixes and suffixes) and then assume that everything before it (with spaces included) must be the "street"?
-
That might work, assuming every address has a type - I'll have to check on that. I've already made a list of all the types that appear in the database, as part of another aspect of the project, so it wouldn't be too difficult to implement. Thanks for the suggestion.
-
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];
}
}
}
?>
-
well done. by the way i know nothing about scripting or programming so the fact my suggestion even made sense is a bonus!
-
well done. by the way i know nothing about scripting or programming so the fact my suggestion even made sense is a bonus!
Can you become a middle manager? Because one of those that also has productive suggestions would make all our lives much easier. ;D