0

I'm parsing an uploaded .csv file, and generating an XML file using a DomDocument in PHP. All is working 100% correctly, wiuth the exception of the very first line of CData that is produced.

If there are 10 lines in the csv, I'm creating 10 lines like the following:

        <ItemData>
          <DataID>0</DataID>
          <Data><![CDATA["Artvan","English","42"]]></Data>
        </ItemData>
        <ItemData>
          <DataID>1</DataID>
          <Data><![CDATA["wallace","English","45"]]></Data>
        </ItemData>
etc .....

Lines 2 and beyond are perfect, but the DataID=0 always has an odd character in front of the first item. "Artvan" is what should be int he above. Notice DataID 1 is just fine? (as would be every one that comes after ...

I'm using the following to generate this line for every single new DataID as it parses through:

            $rec1->appendChild($xml->createCDATASection('"'.trim($element[0]).'","'.trim($element[1]).'","'.trim($element[2]).'"'));

Could this be encoding related? Or something else ? I'm stumped and dont know what to look for.

This is IIS 8.5 with PHP installed , on Windows 2012.

I've not noticed any other issues, with any other parts of the application at all. Just this which is killing me now ;)

Thoughts ?

Vacek
  • 179
  • 1
  • 12
  • 2
    Check to see if the file has a byte order mark (BOM) at the start - https://stackoverflow.com/questions/10290849/how-to-remove-multiple-utf-8-bom-sequences – Nigel Ren May 11 '20 at 18:10
  • Thank you for that. Was exactly the issue and I had never heard of BOM before. Learn something new every day ! :) Appreciate the help !!!!!! – Vacek May 11 '20 at 18:35
  • just as an FYI, here is the way I removed the BOM from my .xml file as it is being created : $rec1->appendChild($xml->createCDATASection('"'.str_replace("\xEF\xBB\xBF","",trim($element[0])).'","'.trim($element[1]).'","'.trim($element[2]).'"')); – Vacek May 11 '20 at 19:39

0 Answers0