Search code examples
phptrim

Stripping a decimal and remaining zeros off an alphanumeric string


I'm returning orders on my success page in magento. The problem is on my quantity (QTY) variable it returns like QTY 2.0000

I've tried several things like $itemQTY_str + 0 which i think is only good for non alphanumeric strings. Additionally, I've tried (floatval)$itemQTY_str

$order=Mage::getModel('sales/order')->getCollection()->addFieldToFilter('increment_id',$this->getOrderId());
$order=$order->getFirstItem();

$order_items=$order->getAllVisibleItems();

$productIds=array();
$itemSKU_str = $itemAMT_str = $itemQTY_str = '';
$i = 1;

foreach($order_items as $order_item) {  
    $_product=Mage::getModel('catalog/product')->load($order_item->getProductId());

    if($_product->getData('fb_pinterest_share')) {
        $productIds[]=$order_item->getProductId();
    }

    if($_SERVER['REMOTE_ADDR'] == '00.00.00.000'){      
        $itemSKU_str = $itemSKU_str . 'ITEM'. $i . '=' . $order_item->getSku() . '&';
        $itemAMT_str = $itemAMT_str . 'AMT'. $i . '=' . $order_item->getPrice() . '&';
        $itemQTY_str = $itemQTY_str . 'QTY'. $i . '=' . $order_item->getQtyOrdered() . '&';
    }

    $i++;

}

/* COMMISSION JUNCTION TRACKING CODE - START */
if($_SERVER['REMOTE_ADDR'] == '00.00.00.000'){
    $order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());

    $con = mysqli_connect('localhost', '******', '*******', '*******');
    if (mysqli_connect_errno()){
        //echo 'Failed to connect to MySQL: ' . mysqli_connect_error();
    }else{  
        $result = mysqli_query($con, "
            SELECT  customer_id
            FROM    sales_flat_order
            WHERE   status = 'complete'
            AND     customer_email LIKE '" . $order->getCustomerEmail() . "'
        ");

        if(mysqli_num_rows($result) > 0){
            //returning customer
            echo '
<iframe height="1" width="1" frameborder="0" scrolling="no" src="https://www.emjcd.com/tags/c?containerTagId=4813&amp;' . $itemSKU_str . $itemAMT_str . (float)$itemQTY_str . 'CID=1527930&amp;OID=' . $this->getOrderId() . '&amp;TYPE=362236&amp;CURRENCY=USD" name="cj_conversion"></iframe>
            ';
        }else{
            //new customer
            echo '
<iframe height="1" width="1" frameborder="0" scrolling="no" src="https://www.emjcd.com/tags/c?containerTagId=4812&amp;' . $itemSKU_str . $itemAMT_str . $itemQTY_str . 'CID=1527930&amp;OID=' . $this->getOrderId() . '&amp;TYPE=362235&amp;CURRENCY=USD" name="cj_conversion"></iframe>
            ';
        }
    }
}

it returns:

<iframe height="1" width="1" frameborder="0" scrolling="no" src="https://www.emjcd.com/tags/c?containerTagId=4813&amp;ITEM1=59444-no-subscription&amp;ITEM2=793573106964-one-time&amp;AMT1=37.0000&amp;AMT2=65.0000&amp;QTY1=2.0000&amp;QTY2=3.0000&amp;CID=1527930&amp;OID=100000928&amp;TYPE=362236&amp;CURRENCY=USD" name="cj_conversion"></iframe>


QTY1=2.0000&amp;QTY2=3.0000

Ultimate goal:

QTY1=2&amp;QTY2=3.

I've also tried editing the string directly..

$itemQTY_str = $itemQTY_str . 'QTY'. $i . + 0 '=' . $order_item->getQtyOrdered() . '&';

$itemQTY_str = $itemQTY_str . 'QTY'. $i . '=' . $order_item->getQtyOrdered(). + 0 . '&';

Hopefully that's clear.. help would be highly appreciated.

Thank you.


Solution

  • Cast the value as an integer.

    $itemQTY_str = $itemQTY_str . 'QTY'. $i . '=' . (int)$order_item->getQtyOrdered() . '&';