0

I am importing user data from a foreign database on demand. While i keep house numbers separate from the street names, the other database does not.

I use

preg_match_all('!\d+!')

To rule out the numbers. This works fine for an addressline like this:

streetname 60

But it does not work for an addressline like this:

streetname 60/2/3

In that case i end up extracting 60, and /2/3 stay in the name of the street.

Unfortunately i am not a regex expert. Quite to the contrary. My problem is that i need to be able to not only detect numerics, but also slashes and hyphens.

Can someone help me out here?

SquareCat
  • 5,699
  • 9
  • 41
  • 75

3 Answers3

5

Try:

preg_match_all('![0-9/-]+!', 'streetname 60/2/3', $matches);

HamZa
  • 14,671
  • 11
  • 54
  • 75
Travis Hegner
  • 2,465
  • 1
  • 12
  • 11
0

to give a definite answer we would have to know the patterns in your data.

for example, in Germany we sometimes have house numbers like 13a or 23-42, which could also be written as 23 - 42

one possible solution would be to match everything after a whitespace that starts with a digit

preg_match_all('!\s(\d.*)!', 'streetname 60/2/3', $matches);

this would produce false positives, though, if you have American data with streets like 13street

cypherabe
  • 2,562
  • 1
  • 20
  • 35
  • That is really good input. As i am dealing with mainly German, Austrian and Swiss street names. However, a problem with using the whitespace as indicator is that it could occur within a street name as well. Perhaps there is a way to use the LAST whitespace BEFORE the first digit, as an indicator? – SquareCat Aug 06 '13 at 18:07
  • this will match exactly that: a whitespace, a digit, and everything following the digit until the end. everything starting with the digit will be considered as part of the group and put in `$matches[1]` (if no flags are set) – cypherabe Aug 07 '13 at 07:37
0

This approach does not use Regex. Will only return when it sees the first number, exploded by space. Ideal for addresses like e.g. 12 Street Road, Street Name 1234B

function getStreetNumberFromStreetAddress($streetAddress){
  $array = explode(' ',$streetAddress);

  if (count($array) > 0){
    foreach($array as $a){
      if (is_numeric($a[0])){
        return $a;
      }
    }
  }
  return null;
}
Pearce
  • 320
  • 4
  • 10