PHP搜索并替换XML

时间:2016-11-28 13:57:05

标签: php xml preg-replace

我有这个XML代码,我想搜索和替换PHPin标签PRICE我想删除点,结果是9600,00。

有什么想法吗?

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

2 个答案:

答案 0 :(得分:0)

您可以尝试以下代码:

renderUI

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

如果您有多个$xml_data = simplexml_load_file('data.xml'); $xml_data = json_decode(json_encode($xml_data), true); $xml_data['PRICE'] = str_replace('.', '', $xml_data['PRICE']); 代码,则以下代码可以使用:

AUCTION

有效。

答案 1 :(得分:-2)

执行此操作的一个简单示例是使用preg_replace_callback

考虑包含您的XML的$str

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