...

View Full Version : dealing with commas within csv elements



civilsurfer
06-01-2011, 06:21 PM
I'm taking a csv file and converting it to xml:



$marks = $dom->createElement('markers');
$dom->appendChild($marks);





// open file to convert to xml
$lines = file("markers.csv");

$count = 0;

foreach($lines as $line) {
echo "<br><br>$line<br>";

list($name,$marker_number,$marker_connect,$marker_connect2,$type,$lat,$lng,$address,$buy_local,$publ ic,$image,$image_big,$web,$connection1,$connection2) = explode(",", $line);

if($count > 0) {
$marker = $dom->createElement('connections');
$marks->appendChild($marker);
$attr1 = $dom->createAttribute('name');
$marker->appendChild($attr1);
$attr2 = $dom->createAttribute('marker_number');
$marker->appendChild($attr2);
$attr3 = $dom->createAttribute('marker_connect');
$marker->appendChild($attr3);
$attr4 = $dom->createAttribute('marker_connect2');
$marker->appendChild($attr4);
$attr5 = $dom->createAttribute('type');
$marker->appendChild($attr5);
$attr6 = $dom->createAttribute('lat');
$marker->appendChild($attr6);
$attr7 = $dom->createAttribute('lng');
$marker->appendChild($attr7);
$attr8 = $dom->createAttribute('address');
$marker->appendChild($attr8);
$attr9 = $dom->createAttribute('buy_local');
$marker->appendChild($attr9);
$attr10 = $dom->createAttribute('public');
$marker->appendChild($attr10);
$attr11 = $dom->createAttribute('image');
$marker->appendChild($attr11);
$attr12 = $dom->createAttribute('image_big');
$marker->appendChild($attr12);
$attr13 = $dom->createAttribute('web');
$marker->appendChild($attr13);
$attr14 = $dom->createAttribute('connection1');
$marker->appendChild($attr14);
$attr15 = $dom->createAttribute('connection2');
$marker->appendChild($attr15);



$txt1 = $dom->createTextNode($name);
$attr1->appendChild($txt1);
$txt2 = $dom->createTextNode($marker_number);
$attr2->appendChild($txt2);
$txt3 = $dom->createTextNode($marker_connect);
$attr3->appendChild($txt3);
$txt4 = $dom->createTextNode($marker_connect2);
$attr4->appendChild($txt4);
$txt5 = $dom->createTextNode($type);
$attr5->appendChild($txt5);
$txt6 = $dom->createTextNode(trim($lat));
$attr6->appendChild($txt6);
$txt7 = $dom->createTextNode(trim($lng));
$attr7->appendChild($txt7);
$txt8 = $dom->createTextNode(trim($address));
$attr8->appendChild($txt8);
$txt9 = $dom->createTextNode($buy_local);
$attr9->appendChild($txt9);
$txt10 = $dom->createTextNode($public);
$attr10->appendChild($txt10);
$txt11 = $dom->createTextNode($image);
$attr11->appendChild($txt11);
$txt12 = $dom->createTextNode($image_big);
$attr12->appendChild($txt12);
$txt13 = $dom->createTextNode(trim($web));
$attr13->appendChild($txt13);
$txt14 = $dom->createTextNode(trim($connection1));
$attr14->appendChild($txt14);
$txt15 = $dom->createTextNode(trim($connection2));
$attr15->appendChild($txt15);

}
$count++;
}

//// output xml to file
echo $dom->save('connections.xml');
;


I'd like to know if there's a simple way to retain commas within the elements of the csv file, without it messing up the structure of the elements (i.e., element 2 = 2622 south street boston, ma
becomes
element 2 = 2622 south street boston
element 3 = ma
)

I'd like to maintain the structure of the code as much as possible and will be using the csv file for folks to input values, and I don't want to put restrictions on them for entering values.

It looks like this works fine with apostrophes so that's good.

Thanks for any help,

Fumigator
06-01-2011, 09:02 PM
The globally accepted solution is to enclose each element in double quotes. If the string contains a double quote it must be escaped with either a second double quote or backslash.

I suppose you could escape a comma that you wanted to consider part of the string, but that's not the standard.

civilsurfer
06-02-2011, 05:12 PM
Do you know offhand where I would double-quote the elements? It would have to be in the csv file correct? And then it would have to be automated as well. I'll look for a way to do this inside excel. Thanks for the help,

Fumigator
06-02-2011, 06:27 PM
Yes, the elements would be double quoted in the CSV file. Excel does it automatically if you save as CSV.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum