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

2
  • Please provide desired input and output Commented Feb 1, 2016 at 10:05
  • They are in the examples image. Source would be the input, string 1 and string 2 would be the 2 output strings Commented Feb 1, 2016 at 10:08

2 Answers 2

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

answered Feb 1, 2016 at 10:12
Sign up to request clarification or add additional context in comments.

Comments

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);
}

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.