gpt4 book ai didi

php - 从字符串中提取地址

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:51:46 25 4
gpt4 key购买 nike

假设我有这个字符串:

<div>john doe is nice guy btw 8240 E. Marblehead Way 92808  is also</div>

或者这个字符串:

<div>sky being blue? in the world is true? 024 Brea Mall  Brea, California 92821 jackfroast nipping on the firehead</div>

我将如何从这些字符串之一中提取地址?这会涉及某种正则表达式,对吧?

我曾尝试在线寻找使用 JavaScript 或 PHP 的解决方案,但无济于事。Stack Overflow 上没有其他帖子(据我所知)提供了使用 jQuery 和/或 Javascript 和/或 PHP 的解决方案。 (最接近的是 Parse usable Street Address, City, State, Zip from a string ,线程中没有任何关于从字符串中提取邮政编码的代码。

有人能指出我正确的方向吗?我将如何在 jQuery 或 JavaScript 或 PHP 中完成此任务?

最佳答案

在 12 个与您的相似的不同琴弦上进行了尝试,效果很好:

function str_to_address($context) { 

$context_parts = array_reverse(explode(" ", $context));
$zipKey = "";
foreach($context_parts as $key=>$str) {
if(strlen($str)===5 && is_numeric($str)) {
$zipKey = $key;
break;
}
}

$context_parts_cleaned = array_slice($context_parts, $zipKey);
$context_parts_normalized = array_reverse($context_parts_cleaned);
$houseNumberKey = "";
foreach($context_parts_normalized as $key=>$str) {
if(strlen($str)>1 && strlen($str)<6 && is_numeric($str)) {
$houseNumberKey = $key;
break;
}
}

$address_parts = array_slice($context_parts_normalized, $houseNumberKey);
$string = implode(' ', $address_parts);
return $string;
}

这里假定门牌号至少为两位数,且不超过六位。这还假定邮政编码不是“扩展”形式(例如 12345-6789)。然而,这可以很容易地修改以适应该格式(正则表达式在这里是一个不错的选择,例如 (\d{5}-\d{4})

但是使用正则表达式来解析用户输入的数据...这不是一个好主意,因为我们不知道用户将要输入什么,因为(正如人们可以假设的那样)没有验证。

遍历代码和逻辑,从从上下文创建数组并获取 zip 开始:

// split the context (for example, a sentence) into an array, 
// so we can loop through it.
// we reverse the array, as we're going to grab the zip first.
// why? we KNOW the zip is 5 characters long*.
$context_parts = array_reverse(explode(" ", $context));

// we're going to store the array index of the zip code for later use
$zipKey = "";

// foreach iterates over an object given the params,
// in this case it's like doing...
// for each value of $context_parts ($str), and each index ($key)
foreach($context_parts as $key=>$str) {

// if $str is 5 chars long, and numeric...
// an incredibly lazy check for a zip code...
if(strlen($str)===5 && is_numeric($str)) {
$zipKey = $key;

// we have what we want, so we can leave the loop with break
break;
}
}

做一些整理,以便我们有更好的对象来装饰门牌号

// remove junk from $context_array, since we don't 
// need stuff after the zip
$context_parts_cleaned = array_slice($context_parts, $zipKey);

// since the house number comes first, let's go back to the start
$context_parts_normalized = array_reverse($context_parts_cleaned);

然后让我们获取门牌号,使用与获取邮政编码相同的基本逻辑:

$houseNumberKey = ""; 
foreach($context_parts_normalized as $key=>$str) {
if(strlen($str)>1 && strlen($str)<6 && is_numeric($str)) {
$houseNumberKey = $key;
break;
}
}

// we probably have the parts we for the address.
// let's do some more cleaning
$address_parts = array_slice($context_parts_normalized, $houseNumberKey);

// and build the string again, from the address
$string = implode(' ', $address_parts);

// and return the string
return $string;

关于php - 从字符串中提取地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14087116/

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