我有magento,我正在通过soap v2 api发布请求以获取订单地址。
有了这个,我得到了以下包含街道名称+门牌号的对象(天知道为什么这些字段不是分开的......)
$shipping_address->street = "4th avenue 108";
现在我想要的是拥有 108 号门牌。
我如何得到这个门牌号码而不得到 4?(如果有人有比我在下面发布的更可靠的功能/代码,请随时发布。)
您基本上需要做的是检查第一个数字是否出现,前面有一个空格。这样可以最大限度地降低获取错误号码的风险:
// Code by Michael Dibbets
// shared under creative commons Attribution 3.0 Unported (CC BY 3.0 http://creativecommons.org/licenses/by/3.0/ )
$value = "4th street26";
$spl = str_split($value);
$pos = 0;
$location = 0;
// simple loop to find the first character with space + integer
foreach($spl as $char)
{
if(is_numeric($char) && $spl[$pos-1]==' ')
{
$location = $pos;
break;
}
$pos++;
}
// If we didn't encounter the space + integer combination
if(!$location)
{
// is the last character an integer? Assume that the last numbers are house numbers
if(is_numeric($spl[count($spl)-1]))
{
for($c=count($spl)-1;$c>0;$c--)
{
if(is_numeric($spl[$c]))
{
continue;
}
else
{
$location = $c+1;
break;
}
}
}
}
if($location)
{
$street = substr($value,0,$location);
$number = substr($value,$location);
}
else
{
$street = $value;
$number = null;
}
// echoing the results. The number should appear after the dash.
echo $street . ' - ' . $number;