0

I have a form where the user can enter his/her address. The street number and street name are entered in the same text field. My database stores the street number directly in the address table, while the street name, city, province, and postal code have their own tables (i.e. they are foreign keys)*.

I want to parse the int from the Street Address field so that I can store the number and street name in their appropriate locations. I referred to the solutions on this page: Extract numbers from a string.

The problem is that if my user accidentally types a number in the street name, using any of the solutions on that page would result in an incorrect street number as well. For example:

123 Som3estreet --> 1233

What is the correct approach here?

*Note: I'm not quite sure if this is the best design, so if anyone has any suggestions for improvement, it would be greatly appreciated.

Community
  • 1
  • 1
Laya302
  • 77
  • 2
  • 7

3 Answers3

2

Addresses, especially international ones are under no obligation to use a consistent format. Some addresses don't even have numbers, but names. For this reason alone I would personally put an end to that endeavor.

However, if you are certain an address will have a number and it is critical to get it right, your best approach will be to require a separate field (form input), for the number, then validate it as such.

EDIT:

A simple organization of addresses may revolve around the first line address and the code. For example, in the UK: 123 Example Road, AC1 2CD

This will be unique in the entire country, and is enough for a local google maps search to find the correct location. So you could create a constraint on this pair.

Flosculus
  • 6,880
  • 3
  • 18
  • 42
  • They are not international addresses, only Canadian addresses will be entered. Hence why I put provinces and no country. – Laya302 Aug 12 '14 at 13:51
  • Then you may have an easier time than most, provided the format of the first line address can be safely assumed. However the solution still applies. – Flosculus Aug 12 '14 at 13:53
  • Is there perchance a 221b Baker Street anywhere in Canada, perhaps in Nelson, BC? – Mark Baker Aug 12 '14 at 13:56
  • 1
    In my experience (working for a shipping company for several years) trying to parse addresses is a never ending hunt for edge cases (in my case the addresses are also not international). Having a single text field for the street, number and other additional info is imo the best option. It will also make your address format compatible with most other things. – Vatev Aug 12 '14 at 13:56
  • The thing is, originally I was just gonna scratch the street name table and store the street address (number and name) directly into my Address table. That way only `city`, `province`, and `postal code` would be foreign keys pointing to their own tables. It's an employee database so there will probably be many duplicates if I did that.. – Laya302 Aug 12 '14 at 13:57
  • @Laya302 stick with your original approach. Duplicating a few addresses is not going to be an issue compared to the massive headache you will get if you try parsing them. – Vatev Aug 12 '14 at 13:58
  • Even normalization of addresses can be a headache. Despite the efforts we made I rarely found an instance where it was actually useful. State/Province, Country sure, but localized parts... and don't get me stated on post/zip codes... – Flosculus Aug 12 '14 at 14:24
0

You need to match from the begin of the string using the ^:

preg_match('/^[0-9]+/', $string, $m);
$number = $match[0];
echo $number; // 123
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
0

It seems like you need to take out the number

$string = '123 Som3estreet';
preg_match_all('!\d+!', $string , $matches);
Anjana Silva
  • 8,353
  • 4
  • 51
  • 54