Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    dealing with commas within csv elements

    I'm taking a csv file and converting it to xml:


    PHP Code:
    $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,$public,$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,

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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,

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Yes, the elements would be double quoted in the CSV file. Excel does it automatically if you save as CSV.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •