0

I'm searching for php regex to split street and house number, but when address hasn't house number it should give only street name.

I have this one:

/([^\d]+)\s?(.+)/i

It works but when an address hasn't a house number, then it splits the last letter from the street name.

This is my code:

$result = mysqli_query($db, "SELECT street FROM branch;");
$row = mysqli_fetch_assoc();

while($row = mysqli_fetch_assoc($result))
{
    if ( preg_match('/([^\d]+)\s?(.+)/i', $row['street'], $street) )
    {
       $streetName = $street[1];
       $streetNumber = $street[2];
       echo $streetName;
       echo "<br />";
    }
}

Examples of addresses: UL.JANA PAWLA, UL.GDANSKA 83A, UL.MOSTOWA 5

pelkas13
  • 75
  • 9
  • the question is unclear. Where is the input coming from and how? – Funk Forty Niner Jan 16 '18 at 13:47
  • 1
    please give example addresses; ones that fail and ones that succeed (or that you want to succeed). Your code block is irrelevant. – Martin Jan 16 '18 at 13:50
  • 2
    `$row = mysqli_fetch_assoc(); while($row = mysqli_fetch_assoc($result)) {` that failed, *outright*. Checking for errors would have told you something about it. – Funk Forty Niner Jan 16 '18 at 13:51
  • 1
    Does it have to be regex? Is it always `streetname number` pattern? Never the other way around? – Andreas Jan 16 '18 at 13:51
  • I'm learning and I think that should be regex, this regex works but only if number exists. – pelkas13 Jan 16 '18 at 13:54
  • the regex works eh? [I doubt that...........](https://stackoverflow.com/questions/48282710/regex-to-address-if-number-exists#comment83549302_48282710). – Funk Forty Niner Jan 16 '18 at 13:55
  • If you're starting out and don't actually have production code out I suggest you redesign your data entry (forms?) and ask for the number and street name individually and store them in the DB individually. If not then play around in https://regex101.com/ – apokryfos Jan 16 '18 at 13:57
  • Just use `\d+` and replace with nothing. It'll extract the numbers from your string and give you the string without the numbers in them – ctwheels Jan 16 '18 at 14:19
  • @ctwheels what about "1st Street 14"? – Andreas Jan 17 '18 at 20:43
  • @Andreas correct, but that's why we don't use regex to parse addresses in the first place. See [how to parse freeform street/postal address out of text, and into components](https://stackoverflow.com/questions/11160192) for more info – ctwheels Jan 17 '18 at 20:48

1 Answers1

1

What I think you're tring to do is:

The Limes 45

becomes:

Street: The Limes
Number: 45

And your issue is that if the data is:

Goblins Only Street

Your output is incorrect.

Street: Globins Only
Number: Street

Therefore: try this Regex:

  \s?([^\d]+\s)\s?(\d+.*)*

What it does:

  • If not a number collects all non-number values as a first group (street)
  • If a number exists after that point then collects the number and all following characters as the second group.

Inputs:

The Limes

Output:

 Street: The Limes  

Inputs:

The Limes 45

Output:

 Street: The Limes  
 Number: 45

Inputs:

The Limes 45B

Output:

 Street: The Limes  
 Number: 45B
Martin
  • 22,212
  • 11
  • 70
  • 132