0

Good day,

I was wondering if you could assist me in someway?

I have a "Product Price" and "Markup" That I want to work out a "Total Price" but it is giving me the incorrect results.

Example: "Total Price" = "Product Price" * "Markup

I have the following code.

<?php 
$markup      = 1 + (15 / 100);
$url         = "http://nanobug.co.za/feed/stock-file/Stock-File-New.xml";
$xml         = simplexml_load_file($url);
foreach ($xml->product as $item)
{
    $product_code  = $item->code;
    $product_price = $item->price;
    $product_price = str_replace("R", "", $product_price);
    $product_price = str_replace(" ", "", $product_price);
    $total_price   = $product_price * $markup;
    echo "Product Price: ".$product_price."<br>";
    echo "Markup: ".$markup."<br>";
    echo "Equation: Total Price = Product Price * Markup<br>";
    echo "Total Price: ".$total_price."<br><br>";
}
?>

The link to see results: View Results

The link to view the xml file: View XML

Can you please help me as it is giving the incorrect total price.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
Andre Hoffmann
  • 362
  • 1
  • 4
  • 12
  • its probably because your changing the value of `$product_price` to a string. – Toxide82 Mar 14 '17 at 09:18
  • @ÁlvaroGonzález I think that OP has fixed the script online so now the edited question in fact contains the correct results... – ewcz Mar 14 '17 at 09:43
  • @ewcz Dammit... – Álvaro González Mar 14 '17 at 09:43
  • @Andre Can you please edit the question and share the actual code that exhibits the issue? That's why site guidelines mandate that questions are self-contained. Yours broke in less than an hour :) – Álvaro González Mar 14 '17 at 09:44
  • @ÁlvaroGonzález also, the copy-pasted XML does not seem to contain the non-breaking space which was the cause of the problem (it looks like it was replaced with an ordinary one) – ewcz Mar 14 '17 at 09:47
  • The price did contain a non-breaking space witch was the problem. @ewcz have the correct solution and it is working 100% replace **$product_price = str_replace(" ", "", $product_price);** with **$product_price = preg_replace('~[ \x{00a0}]~siu', '', $product_price);** – Andre Hoffmann Mar 14 '17 at 09:49
  • @ewcz I kept the link to the original just in case. Of course, there's no way to say whether the linked file has been replaced meanwhile. Anyway, I've finally reverted my changes, let's see if Andre fixes the question. – Álvaro González Mar 14 '17 at 09:57

1 Answers1

2

I would suggest to use

$product_price = preg_replace('~[ \x{00a0}]~siu', '', $product_price);

instead of

$product_price = str_replace(" ", "", $product_price);

The reason for this is that your xml file looks like:

grep price *.xml | head -n1 | hexdump -C

00000000  20 20 20 20 3c 70 72 69  63 65 3e 52 20 32 38 c2  |    <price>R 28.|
00000010  a0 39 39 39 2e 30 30 3c  2f 70 72 69 63 65 3e 0d  |.999.00</price>.|
00000020  0a                                                |.|
00000021

i.e., the space serving as a delimiter of the thousands is not an ordinary space but a non-breaking space (C2 A0 in UTF-8 as shown above), thus the statement str_replace(" ", "", $product_price) had no effect on it and therefore you were effectively taking into account only the thousands (i.e., in this case "28 999"*1.15 which yields 28*1.15)...

Community
  • 1
  • 1
ewcz
  • 12,819
  • 1
  • 25
  • 47