alexius
alexius

Reputation: 73

PHP - XML to CSV conversion issue

I really need some help on an issue: I'm converting an XML to CSV using a PHP script, but the XML contains some + signs and I don't know how to remove them from the CSV.

This is the XML file structure:

<PRICES>
<PRICE>
<WIC>HDE0AAFGRBOX</WIC>
<STOCK>100+</STOCK>
<MY_PRICE>219.00</MY_PRICE>
</PRICE>
</PRICES>

This is the script I use:

<?
$filexml='stock.xml';
if (file_exists($filexml)) {
$xml = simplexml_load_file($filexml);
$f = fopen('stock.csv', 'w')
foreach($xml->PRICES->PRICE as $price) {
fputcsv($f, get_object_vars($price),',','"');
}
fclose($f);
}
?>

The script works fine and the CSV file is good, but because I'm a noob with PHP I don't know how to remove the "+" sign.

Any help will be greatly appreciated.

Thanks in advance!

Upvotes: 0

Views: 707

Answers (2)

JuSchz
JuSchz

Reputation: 1198

Try to use str_replace :

$filexml='stock.xml';
if (file_exists($filexml)) {
$xml = simplexml_load_file($filexml);
$f = fopen('stock.csv', 'w')
foreach($xml->PRICES->PRICE as $price) {
$price->STOCK = str_replace("+","",$price->STOCK);
fputcsv($f, get_object_vars($price),',','"');
}
fclose($f);
}

Upvotes: 0

Stelian Matei
Stelian Matei

Reputation: 11623

You could try to trim the chars you want to be removed (the '+' char):

foreach($xml->PRICES->PRICE as $price) {
   $price->STOCK = trim($price->STOCK, "+");
   fputcsv($f, get_object_vars($price),',','"');
}

Upvotes: 1

Related Questions