繁体   English   中英

如何在php中删除CSV文件中的空白行

[英]How to remove blank line in CSV file in php

我试图从xml文件中获取数据然后将其写入CSV文件。在生成的文件中,它在每个记录后放了这么多空行。

<?php

header("Content-Type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=spreadsheet.xls");
$url = 'test.xml'; // xml file location with file name
if (file_exists($url)) {
    $xml = simplexml_load_file($url);
    echo 'item_no' . "\t" . 'description' . "\t" . 'price' . "\t" . 'base_qty' . "\t" . 'var_qty' . "\t" . 'base_price_1' . "\t\n";
    foreach ($xml->record as $books) {
        $array_count = count($books);
        for ($key = 0; $key < $array_count; $key++) {
            echo $books->item_no[$key]."\t" . $books->description[$key]."\t" . $books- >price[$key]."\t" . $books->base_qty[$key] . "\t" . $books->var_qty[$key] . "\t" . $books->base_price_1[$key] . "\t" . "\n";
        }
    }
}
?>

确保\\n未在最后一条记录中打印

<?php

header("Content-Type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=spreadsheet.xls");
$url = 'test.xml'; // xml file location with file name
$output = "";
if (file_exists($url)) {
    $xml = simplexml_load_file($url);
    $output.= 'item_no' . "\t" . 'description' . "\t" . 'price' . "\t" . 'base_qty' . "\t" . 'var_qty' . "\t" . 'base_price_1' . "\t\n";
    foreach ($xml->record as $books) {
        $array_count = count($books);
        for ($key = 0; $key < $array_count; $key++) {
            $output.= $books->item_no[$key]."\t" . $books->description[$key]."\t" . $books- >price[$key]."\t" . $books->base_qty[$key] . "\t" . $books->var_qty[$key] . "\t" . $books->base_price_1[$key] . "\t" . "\n";
        }
    }
    echo rtrim($output);
}
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM