简体   繁体   English

PHP csv 文件以列和行的形式导出

[英]PHP csv file exported with results in columns and rows

What I'm trying to do is with this PHP script is read a specific code that has different results depending on the digit in its location.我正在尝试使用此 PHP 脚本读取特定代码,该代码根据其位置中的数字具有不同的结果。
So the code starts in position 709 in the txt file and looks like: FRNARUFY16UEAE5QC所以代码从 txt 文件中的 position 709 开始,看起来像: FRNARUFY16UEAE5QC
that's all fine and good and I get my results that I want my problem arises in the exported CSV file.这一切都很好,我得到了我希望我的问题出现在导出的 CSV 文件中的结果。
I managed to get each result to be displayed on a new line with \r\n but I don't know how to get each result to be on a new column.我设法用 \r\n 将每个结果显示在新行上,但我不知道如何让每个结果显示在新列上。
Ideally I wish to make the final result like this理想情况下,我希望做出这样的最终结果在此处输入图像描述

I have tried a few solutions I found but none have worked and I suspect it's the fault of my code rather than the solutions themselves so any help would be appreciated.我尝试了一些我发现的解决方案,但没有一个有效,我怀疑这是我的代码的错,而不是解决方案本身的错,所以任何帮助都将不胜感激。

<?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;




?>

You are nearly there,你快到了,

CSV files are super basic, and it actually stands for Comma, Separated, Value. CSV 文件是超级基本的,它实际上代表逗号,分隔,值。

So to go to the next column, you simply add a comma to separate into columns.因此,对于 go 到下一列,您只需添加一个逗号以分隔成列。

echo $vinsToUpdate ="Vin ".substr($line,10,17), " F16, 1, Petrol, 8703211000, 1211, 1211, uk";

etc ETC

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

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