如何通过肥皂检索magento街道价值的门牌号码?

时间:2013-04-04 07:40:19

标签: php string magento

我有magento,我通过soap v2 api发布请求以获取订单的地址。

有了这个,我得到了以下包含街道名称+ housenumber的对象(天知道为什么这些字段不是单独的......)

$shipping_address->street = "4th avenue 108";

现在我想要的是拥有房屋号码108.

如何在不获得4号码的情况下获得此号码? (如果某人有比我下面发布的更可靠的功能/代码,请随时发布。)

1 个答案:

答案 0 :(得分:0)

您基本上要做的是检查第一个出现的数字,前面有一个空格。 这样可以最大程度地降低获取错误号码的风险:

// 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;