0

I have this XML code and i want to search and replace with PHPin tag PRICE i want to remove point and the result to be 9600,00.

Any idea ?

<AUCTION>
<UNIQUEID>36048</UNIQUEID>
<TITLE>UPS APC SMartUPS 20kVA SUVT20KH4B4S</TITLE>
<DESCRIPTION>UPS-uri &amp; Stabilizatoare &gt; UPS-uri ReconditionateUPS APC SMartUPS 20kVA SUVT20KH4B4SPretul afisat este fara BateriiAn de fabricatie 2006Functioneaza cu 128 acumulatori de 12V 7AEste inclus in pret modulul de bypassAPC Smart-UPS VT SUVT20KH4B4S 20 kVAInput400V 3PH / Output400V 3PH Interface Port DB-9 RS-232, Smart-Slot Extended runtime model.. Model : cu managment   Dimensiuni : 1499 x 559 x 813 mm, Greutate : 600 Kg, Putere : 20000 Va</DESCRIPTION>
<PRICE>9.600,00</PRICE>
<CURRENCY>RON</CURRENCY>

<CATEGORY>UPS-uri &amp; Stabilizatoare &gt; UPS-uri Reconditionate</CATEGORY>


<AMOUNT>0</AMOUNT>
<PHOTOS><URL>https://shoplaptop.ro/image/data/poze_VM/20130709_154155_51DCA400_5377F800.jpg</URL></PHOTOS>
<WARRANTY>1</WARRANTY>
<STATE>2</STATE>
</AUCTION>
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Velicu Cristian
  • 27
  • 1
  • 10

2 Answers2

0

You could try the following code:

$xml_data = simplexml_load_file('data.xml');
$xml_data->PRICE = str_replace('.', '', $xml_data->PRICE);

OR

$xml_data = simplexml_load_file('data.xml');
$xml_data = json_decode(json_encode($xml_data), true);
$xml_data['PRICE'] = str_replace('.', '', $xml_data['PRICE']);

The following code would work if you have more than one AUCTION tag:

$xml_data = simplexml_load_file('data.xml');
$xml_data = json_decode(json_encode($xml_data), true);
foreach($xml_data as &$xml_data_value) {
    $xml_data_value['PRICE'] = str_replace('.', '', $xml_data_value['PRICE']);
}

It works.

Wolverine
  • 1,712
  • 1
  • 15
  • 18
-2

A simple example to do this is using preg_replace_callback:

Consider $str containing your XML:

$str = <<<S
<AUCTION>
<UNIQUEID>36048</UNIQUEID>
<TITLE>UPS APC SMartUPS 20kVA SUVT20KH4B4S</TITLE>
<DESCRIPTION>UPS-uri &amp; Stabilizatoare &gt; UPS-uri ReconditionateUPS APC SMartUPS 20kVA SUVT20KH4B4SPretul afisat este fara BateriiAn de fabricatie 2006Functioneaza cu 128 acumulatori de 12V 7AEste inclus in pret modulul de bypassAPC Smart-UPS VT SUVT20KH4B4S 20 kVAInput400V 3PH / Output400V 3PH Interface Port DB-9 RS-232, Smart-Slot Extended runtime model.. Model : cu managment   Dimensiuni : 1499 x 559 x 813 mm, Greutate : 600 Kg, Putere : 20000 Va</DESCRIPTION>
<PRICE>9.600,00</PRICE>
<CURRENCY>RON</CURRENCY>

<CATEGORY>UPS-uri &amp; Stabilizatoare &gt; UPS-uri Reconditionate</CATEGORY>


<AMOUNT>0</AMOUNT>
<PHOTOS><URL>https://shoplaptop.ro/image/data/poze_VM/20130709_154155_51DCA400_5377F800.jpg</URL></PHOTOS>
<WARRANTY>1</WARRANTY>
<STATE>2</STATE>
</AUCTION>
S;

$str = preg_replace_callback('@<(price)>(.*?)</\1>@im', function($res) {
    return str_replace('.', '', $res[0]);
}, $str);

echo $str;
ar34z
  • 2,609
  • 2
  • 24
  • 37