PHP数组拒绝将数字字符串视为数字值

时间:2012-07-26 21:01:58

标签: php xml arrays curl var

我正在尝试将PHP数组输出转换为正数PHP变量,以便我可以对其进行算术运算,然后将其全局存储以用于购物车。

以下是生成和操作数组的代码:

<?php
function ups($dest_zip,$service,$weight,$length,$width,$height) {
$AccessLicenseNumber = 'XXXXXXXXXXXXX'; // Your license number
$UserId = 'XXXXXXXX'; // Username
$Password = 'XXXXXXXX'; // Password
$PostalCode = 'XXXXXX'; // Zipcode you are shipping FROM
$ShipperNumber = 'XXXXX'; // Your UPS shipper number

    $data ="<?xml version=\"1.0\"?>
    <AccessRequest xml:lang=\"en-US\">
        <AccessLicenseNumber>$AccessLicenseNumber</AccessLicenseNumber>
        <UserId>$UserId</UserId>
        <Password>$Password</Password>
    </AccessRequest>
    <?xml version=\"1.0\"?>
    <RatingServiceSelectionRequest xml:lang=\"en-US\">
        <Request>
            <TransactionReference>
                <CustomerContext>Bare Bones Rate Request</CustomerContext>
                <XpciVersion>1.0001</XpciVersion>
            </TransactionReference>
            <RequestAction>Rate</RequestAction>
            <RequestOption>Rate</RequestOption>
        </Request>
    <PickupType>
        <Code>01</Code>
    </PickupType>
    <Shipment>
        <Shipper>
            <Address>
                <PostalCode>$PostalCode</PostalCode>
                <CountryCode>US</CountryCode>
            </Address>
        <ShipperNumber>$ShipperNumber</ShipperNumber>
        </Shipper>
        <ShipTo>
            <Address>
                <PostalCode>$dest_zip</PostalCode>
                <CountryCode>US</CountryCode>
            <ResidentialAddressIndicator/>
            </Address>
        </ShipTo>
        <ShipFrom>
            <Address>
                <PostalCode>$PostalCode</PostalCode>
                <CountryCode>US</CountryCode>
            </Address>
        </ShipFrom>
        <Service>
            <Code>$service</Code>
        </Service>
        <Package>
            <PackagingType>
                <Code>02</Code>
            </PackagingType>
            <Dimensions>
                <UnitOfMeasurement>
                    <Code>IN</Code>
                </UnitOfMeasurement>
                <Length>$length</Length>
                <Width>$width</Width>
                <Height>$height</Height>
            </Dimensions>
            <PackageWeight>
                <UnitOfMeasurement>
                    <Code>LBS</Code>
                </UnitOfMeasurement>
                <Weight>$weight</Weight>
            </PackageWeight>
        </Package>
    </Shipment>
    </RatingServiceSelectionRequest>";
    $ch = curl_init("https://www.ups.com/ups.app/xml/Rate");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_TIMEOUT, 60);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    $result=curl_exec ($ch);        
   $upsout = (explode('USD', $result, 15));    
    $handling = $upsout[3];
   echo "<br><br>Shipping: $";
    echo $handling; 
   echo " USD<br><br>";       
   curl_close($ch);
   var_dump( $handling);
// print_r ($upsout);}?>

$ handling的输出始终是00.00或000.00格式的数字,其中零是数字。我真的需要它能够在算术函数中工作,并且能够存储以用作数字。

如果我这样做,我知道它不起作用

$test = $handling *2;
echo $test;

我得到“0”作为输出,而不是任何$ handling处理的双倍......

编辑:

如果我运行var_dump(htmlentities($ handling));

输出是:

string(222)“33.35”

如果我运行var_dump($ handling);

输出

string(168)“33.35”

所以....我需要找到一种方法来解析这个数组条目中的垃圾,这样我就可以代数地操作它。

3 个答案:

答案 0 :(得分:1)

我认为你的字符串中包含HTML标记,这就是你得到这个var_dump()的原因:

string(168) "33.35"

第一个数字表示您的字符串长度为168个字符,而33.35占用5个字符。所以那里还有别的东西,不仅仅是一个数字。

其中一个解决方案可行,但我无法确定字符串的实际外观,所以我抛弃了一堆想法。

$haystack0 = strip_tags( $haystack); 
echo '0. ' . $haystack0 * 2; echo "<br />\n";

$haystack1 = trim( strip_tags( $haystack)); 
echo '1. ' . $haystack1 * 2; echo "<br />\n";

preg_match( '/\b(\d+\.\d+)\b/', $haystack, $match);
$haystack2 = $match[1];
echo '2. ' . $haystack2 * 2; echo "<br />\n";

$haystack3 = preg_replace( '/[^\d\.]+/', '', $haystack); 
echo '3. ' . $haystack3 * 2; echo "<br />\n";

编辑:以下是实际字符串:

string(222) "</CurrencyCode><MonetaryValue>33.35</MonetaryValue></TotalCharges><GuaranteedDaysToDelivery/><ScheduledDeliveryTime/><RatedPackage><TransportationCharges><CurrencyCode>"

因此,我们将其放入$haystack并删除XML标记:

$haystack = "</CurrencyCode><MonetaryValue>33.35</MonetaryValue></TotalCharges><GuaranteedDaysToDelivery/><ScheduledDeliveryTime/><RatedPackage><TransportationCharges><CurrencyCode>";
$haystack = strip_tags( $haystack);
var_dump( $haystack);

我们得到as output

 string(5) "33.35"

所以,现在,我们可以将它乘以and get

 float(66.7)

问题解决了!

答案 1 :(得分:0)

我刚试过这个并且工作正常,但是你会丢失小数值:

<?php
$handling = '12.24';

echo intval($handling) * 2; // prints 24
echo floatval($handling) * 2; // prints 24.48

?>

要解决您的问题,您应该使用floatval()函数包装$ handling:

<?php function ups($dest_zip,$service,$weight,$length,$width,$height)
    </RatingServiceSelectionRequest>";
    $ch = curl_init("https://www.ups.com/ups.app/xml/Rate");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_TIMEOUT, 60);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    $result=curl_exec ($ch);        
    $upsout = (explode('USD', $result, 15));    
    $handling = floatval($upsout[3]);  // <--- recommend wrap here if you need to use variable later in math
    echo "<br><br>Shipping: $";
    echo $handling;
    echo " USD<br><br>";       
    curl_close($ch);
    // print_r ($upsout);}
 ?>

答案 2 :(得分:0)

编辑:考虑到这一点,你确定那些带有$ test的示例代码实际上是这样发生的吗? Type juggling应允许您将字符串乘以数字并返回一个数字。如果您将示例代码的测试行更改为:

,会发生什么
print "handling is $handling \n calculated value is $test"; 

原文:由于这似乎是您从UPS返回的费用,因此很可能不会是整数,因为它会有分数(您提到的价值是形式00.00000.00似乎也证实了这一点。

因此,您需要使用以下方法之一将其变为浮点数:

$test = (float)$handling * 2;
$test2 = floatval($handling) * 2;