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.

5
  • Use a conditional statement with your field while using is_numeric() Commented Aug 12, 2014 at 13:44
  • intval? Commented Aug 12, 2014 at 13:44
  • Kindly elaborate, I'm new at this.. Commented Aug 12, 2014 at 13:45
  • The function I'm talking about can be seen here > php.net/manual/en/function.is-numeric.php Commented Aug 12, 2014 at 13:46
  • You can find the part of the input which matches an existing street name, and treat whatever is left as a street number. Commented Aug 12, 2014 at 13:49

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

They are not international addresses, only Canadian addresses will be entered. Hence why I put provinces and no country.
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.
Is there perchance a 221b Baker Street anywhere in Canada, perhaps in Nelson, BC?
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.
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..
|
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

Comments

0

It seems like you need to take out the number

$string = '123 Som3estreet';
preg_match_all('!\d+!', $string , $matches);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.