开发者

tab-delimited string to XML with PHP

I'm trying to write the tab-delimited content of a variable to XML like this:

$tsvData = str_getcsv($input, "\t");
foreach($tsvData as $line => $row) {
    if($line > 0) {
        $xmlWriter->writeElement('NAM开发者_运维技巧E', $row[0]);
        $xmlWriter->writeElement('CAKE', $row[1]);
        $xmlWriter->writeElement('BODYPART', $row[2]);
    }
}

But it's only writing one character per XML tag instead of everything between each tab. When I use SplFileObject, geting the same tsv data but from a file, it works. What am I doing wrong with the str_getcsv function?

Thanks


The str_getcsv() function returns a 1-dimensional array, but you're treating it like it's returning a 2-dimensional array.

Edit:

To clarify, str_getcsv() has no concept of "lines". Instead of doing this:

$tsvData = str_getcsv($input, "\t");

Thinking that you'll get an array of lines, each one containing an array of columns, you have to do something like this:

$lines = explode("\n", $input);
$tsvData = array();
foreach ($lines as $line) {
    $tsvData[] = str_getcsv($line, "\t");
}
// now $tsvData is a 2-dimensional array of lines/columns like you were wanting
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜