开发者

How do I parse the free format address to save into the DataBase

开发者 https://www.devze.com 2022-12-22 10:48 出处:网络
I have a text area that allow user the type in an address in free format, how do I p开发者_StackOverflow社区arse the address user entered into address1, address2, city, state, zip and country and save

I have a text area that allow user the type in an address in free format, how do I p开发者_StackOverflow社区arse the address user entered into address1, address2, city, state, zip and country and save into DB?


You are in for a lot of pain here.

Addresses REALLY BADLY differ between countries, and even within one country parsing is error prone. Been there, leading a team of 2 people spending months trying to get all GERMAN addresses to parse. Lots of stupid little specialties left and right.

Sad news - I dont think that can be done reliably. Not oif the entry is free form, not with the way people enter addresses in various ways.

Go with a decent entry form, not with free text entry.


Don't, go with a separate-field data entry form instead.

Assuming you don't/can't/won't heed that advice, then you have to make some assumptions about how it's been entered. You can be a little clever by start from the ends and work your way in, though.

I.e:

  • Split by new lines.
  • 1st line is housenumber and street.
  • Last line could be postcode/zipcode.
  • Last-but-one line could be city.
  • Lines in-between first and last-but-one are address line 2, 3 etc.

IMPORTANT
Address formats vary wildly between countries. If I were you and you have the scope for it, I'd at least provide a country drop-down so you can apply different parsing rules for each country.


I do not think you will be able to parse it correctly in case of free format. You may use MaskedEdit for these purposes. Another way is to require user to enter information in certain order.


You can't predict how a user is going to enter the address e.g. some prefer using newlines, others prefer using comma's.

To save the hassle I would just have separate fields for each individual item, or just save the address as a whole string.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号