Search code examples
phpregexpreg-match

PHP - Split street name and street address


I need to strip street numbers from street name. I face the problem when there is no numerical part like Avenue ABC or when street name starts with number like 10. Street 46.

My regex right now is:

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

These are test strings:

St. Stephen 12
Avenue ABC
Avenue AB
10. Street 46

Result should looks like:

array = (0 => 'St. Stephen', 1 => '12')
array = (0 => 'Avenue', 1 => 'ABC')
array = (0 => 'Avenue', 1 => 'AB')
array = (0 => '10. Street', 1 => '46')

Live regex: http://www.phpliveregex.com/p/cil


Solution

  • You may use the below regex which produces two captures of one contains all the chars upot the last space where another group contain last word.

    ^(.+)\s(.+)
    

    DEMO