0

I've been looking around for some help, but haven't found my solution yet. The problem is a lot like this one: Extract address from string

But I cannot seem to rewrite the php code to solve my problem.

I'm using Magento and I have only 1 address field combining streetname and housenumber. For my CSV export I'm using a XSLT extension, which can work with PHP also. For retrieving the address and importing, I need streetname and housenumber to be 2 strings.

At this moment I'm using: preg_replace('/[0-9,.]/','',$address);

for retrieving the street,

and: preg_replace('/[^0-9,.]/','',$address);

for retrieving the housenumber. And... this just doesn't work in a lot of situations. Because sometimes a number is included in the streetname (like 2nd street) or a character is included in the housenumber (like 36-B).

The only 2 things we always know are "A housenumber always includes a number" and "A housenumber (sometimes including characters) is always at the end of the string"

I've made an image of a few examples: Examples

Community
  • 1
  • 1

2 Answers2

0

You will have to search for the last number in the string. Than the first space before that, and split it at this position

Doktor OSwaldo
  • 5,732
  • 20
  • 41
0

I found the following code to work almost perfect.

static function addressFix($address){
    $r = strrev ($address);
    $str1= preg_replace('/^(.*?\d+)(.*?)$/', '$2', $r);
    return strrev($str1);
}

static function houseNumberFix($address){
    $r = strrev ($address);
    $str2= preg_replace('/^(.*?\d+)(.*?)$/', '$1', $r);
    return strrev($str2);
}