View Full Version : dealing with commas within csv elements

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

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

// 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');
$attr1 = $dom->createAttribute('name');
$attr2 = $dom->createAttribute('marker_number');
$attr3 = $dom->createAttribute('marker_connect');
$attr4 = $dom->createAttribute('marker_connect2');
$attr5 = $dom->createAttribute('type');
$attr6 = $dom->createAttribute('lat');
$attr7 = $dom->createAttribute('lng');
$attr8 = $dom->createAttribute('address');
$attr9 = $dom->createAttribute('buy_local');
$attr10 = $dom->createAttribute('public');
$attr11 = $dom->createAttribute('image');
$attr12 = $dom->createAttribute('image_big');
$attr13 = $dom->createAttribute('web');
$attr14 = $dom->createAttribute('connection1');
$attr15 = $dom->createAttribute('connection2');

$txt1 = $dom->createTextNode($name);
$txt2 = $dom->createTextNode($marker_number);
$txt3 = $dom->createTextNode($marker_connect);
$txt4 = $dom->createTextNode($marker_connect2);
$txt5 = $dom->createTextNode($type);
$txt6 = $dom->createTextNode(trim($lat));
$txt7 = $dom->createTextNode(trim($lng));
$txt8 = $dom->createTextNode(trim($address));
$txt9 = $dom->createTextNode($buy_local);
$txt10 = $dom->createTextNode($public);
$txt11 = $dom->createTextNode($image);
$txt12 = $dom->createTextNode($image_big);
$txt13 = $dom->createTextNode(trim($web));
$txt14 = $dom->createTextNode(trim($connection1));
$txt15 = $dom->createTextNode(trim($connection2));


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

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.

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,

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