簡體   English   中英

csv 文件的最后一行

[英]last line of a csv file

晚上好,我想從你那里得到一點 CSV 文件的幫助。

我的代碼是這樣的。

<?php

$line = '';
$f = fopen('C:\Users\User\Documents\OMRON FZ\USBDisk\Conteggio.csv', 'r');
$cursor = -1;
fseek($f, $cursor, SEEK_END);
$char = fgetc($f);
//Trim trailing newline characters in the file
while ($char === "\n" || $char === "\r") {
   fseek($f, $cursor--, SEEK_END);
   $char = fgetc($f);
}
//Read until the next line of the file begins or the first newline char
while ($char !== false && $char !== "\n" && $char !== "\r") {
   //Prepend the new character
   $line = $char . $line;
   fseek($f, $cursor--, SEEK_END);
   $char = fgetc($f);
}
echo $line;

?>

CSV 數據保存在一行中。 例如:ROWS1、RESULT1、RESULT2、RESULT3、

我是否可以決定向自己展示保存在同一行的唯一結果,例如僅打印 RESULT3?

我想獲得的最終結果

使用file()將文件讀入行數組。 最后一行將是數組的最后一個元素。 然后您可以使用str_getcsv()將其解析為字段並獲取最后一個字段。

$lines = file('C:\Users\User\Documents\OMRON FZ\USBDisk\Conteggio.csv', FILE_IGNORE_NEW_LINES);
if (!empty($lines)) {
    $fields = str_getcsv($lines[count($lines)-1]); // get fields from last line
    echo $fields[count($fields)-1]; // print last field
} else {
    echo "CSV is empty";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM