[英]PHP csv file exported with results in columns and rows
我正在尝试使用此 PHP 脚本读取特定代码,该代码根据其位置中的数字具有不同的结果。
所以代码从 txt 文件中的 position 709 开始,看起来像: FRNARUFY16UEAE5QC
这一切都很好,我得到了我希望我的问题出现在导出的 CSV 文件中的结果。
我设法用 \r\n 将每个结果显示在新行上,但我不知道如何让每个结果显示在新列上。
理想情况下,我希望做出这样的最终结果
我尝试了一些我发现的解决方案,但没有一个有效,我怀疑这是我的代码的错,而不是解决方案本身的错,所以任何帮助都将不胜感激。
<?php
$files = glob('C:/Users/X/Desktop/php/in/file*.{txt}', GLOB_BRACE);
$vinsToUpdate = array();
if ( $files !== false )
{
$filecount = count( $files );
foreach($files as $file)
{
$handle = fopen($file, "r");
if ($handle)
{
while (($line = fgets($handle)) !== false)
{
if(strlen($line) > 1)
{
$csv = str_getcsv($line);
if(substr($line,708,1) === 'F' && substr($line,709,2) === 'RN' && substr($line,715,1) === 'Y')
{
echo $vinsToUpdate ="Vin ".substr($line,10,17), " F16 1 Petrol 8703211000 1211 1211 uk \r\n";
}
if(substr($line,708,1) === 'F' && substr($line,709,2) === 'RN' && substr($line,715,1) === 'L')
{
echo $vinsToUpdate ="Vin ".substr($line,10,17), " F16 1 Petrol 8703211000 1159 1159 uk \r\n";
}
if(substr($line,708,1) === 'F' && substr($line,709,2) === 'SD' && substr($line,715,1) === '9' && substr($line,716,4) === 'ZE16')
{
echo $vinsToUpdate ="Vin ".substr($line,10,17), " ZE1 40kwh Electric 8703801000 1515 1515 UK \r\n";
}
if(substr($line,708,1) === 'F' && substr($line,709,2) === 'SD' && substr($line,715,1) === '9' && substr($line,716,4) === 'ZE18')
{
echo $vinsToUpdate ="Vin ".substr($line,10,17), " ZE1 60kwh Electric 8703801000 1708 1708 UK \r\n";
}
if(substr($line,708,1) === 'T' && substr($line,709,2) === 'DZ' && substr($line,715,1) === 'Y')
{
echo $vinsToUpdate ="Vin ".substr($line,10,17), " J12 1.3 Petrol 8703221000 1466 1466 UK \r\n";
}
if(substr($line,708,1) === 'T' && substr($line,709,2) === 'DZ' && substr($line,715,1) === 'W')
{
echo $vinsToUpdate ="Vin ".substr($line,10,17), " J12 1.3 Petrol 8703221000 1511 1511 UK \r\n";
}
if(substr($line,708,1) === 'T' && substr($line,709,2) === 'DZ' && substr($line,715,1) === 'W')
{
echo $vinsToUpdate ="Vin ".substr($line,10,17), " J12 1.3 Petrol 8703221000 1570 1570 UK \r\n";
}
}
}
fclose($handle);
} else {
// error opening the file.
}
}
}
// Excel file name for download
$fileName = "Spec" . ".csv";
// Headers for download
header("Content-Disposition: attachment; filename=\"$fileName\"");
header("Content-Type: application/vnd.ms-excel");
exit;
?>
你快到了,
CSV 文件是超级基本的,它实际上代表逗号,分隔,值。
因此,对于 go 到下一列,您只需添加一个逗号以分隔成列。
echo $vinsToUpdate ="Vin ".substr($line,10,17), " F16, 1, Petrol, 8703211000, 1211, 1211, uk";
ETC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.