从Addressfield拆分房屋号码

时间:2015-03-04 15:22:02

标签: php mysql logic

我想将一个地址字段拆分为街道和房屋字段。

这是一些示例数据:

Examplestreet 1
Examplestreet 1A
Examplestreet 1 B
The Examplest street 2
1st Example street 3A
1st Example street 13 A  

现在,我想我从右边开始,寻找我遇到的第一个数字,然后继续前进,直到第一个空间被吸引并分裂。

你会得到这样的东西:

Example:                           1st Example street 13 A
Start from the right:              A
Find the first number:             A 3
Keep going until the first space:  A 31
Split here:                        1st Example Street    |     13A 

我希望单独使用mySQL,但也可以使用PHP。

当你知道更好的方法时,我想知道。

我开始关注SUBSTRING_INDEX,但这并不能解决问题。

坦率地说,我还不知道从哪里开始。

1 个答案:

答案 0 :(得分:1)

如果门牌号始终位于字符串的末尾,从数字开始,我们可以使用正则表达式:

<?php
$names = array(
    'Examplestreet 1',
    'Examplestreet 1A',
    'Examplestreet 1 B',
    'The Examplest street 2',
    '1st Example street 3A',
    '1st Example street 13 A ',
    );

foreach($names as $value)
{
    $matches = array();
    if (preg_match('/.*\s(\d.*)/', $value, $matches))
    {
        print $matches[1]."\n";
    }
}

输出:

1
1A
1 B
2
3A
13 A