gpt4 book ai didi

php - 从地址字段中拆分门牌号

转载 作者:行者123 更新时间:2023-11-29 00:02:59 25 4
gpt4 key购买 nike

我想将单个地址字段拆分为街道和门牌号字段。

这是一些示例数据:

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,但没有成功。

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

最佳答案

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

<?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

关于php - 从地址字段中拆分门牌号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28858135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com