Skip to content
Advertisement

Getting zipcode from a string

I am trying to get zipcodes out of address strings.

Zipcodes may look like this: 23-123 or 50-530.

Strings usually look like this: Street 50, 50-123 City

What I tried to do is finding the position of the zipcode and cut the next 6 characters starting from that point. Unfortunatelly strpos returns false all the time.

JavaScript

Advertisement

Answer

The strpos does not allow the use of regex as an argument.

You need a preg_match / preg_match_all here:

JavaScript

The regex matches

  • b – a word boundary
  • d{2} – two digits
  • - – a hyphen
  • d{3} – three digits
  • b – a word boundary

See the regex demo.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement