【问题标题】:How to delete first 11 lines in a file using PHP?如何使用 PHP 删除文件中的前 11 行?
【发布时间】:2019-04-12 00:09:59
【问题描述】:

我有一个 CSV 文件,我希望删除其中的前 11 行。该文件类似于:

"MacroTrends Data Download"
"GOOGL - Historical Price and Volume Data"
"Historical prices are adjusted for both splits and dividends"

"Disclaimer and Terms of Use: Historical stock data is provided 'as is' and solely for informational purposes, not for trading purposes or advice."
"MacroTrends LLC expressly disclaims the accuracy, adequacy, or completeness of any data and shall not be liable for any errors, omissions or other defects in, "
"delays or interruptions in such data, or for any actions taken in reliance thereon.  Neither MacroTrends LLC nor any of our information providers will be liable"
"for any damages relating to your use of the data provided."


date,open,high,low,close,volume
2004-08-19,50.1598,52.1911,48.1286,50.3228,44659000
2004-08-20,50.6614,54.7089,50.4056,54.3227,22834300
2004-08-23,55.5515,56.9157,54.6938,54.8694,18256100
2004-08-24,55.7922,55.9728,51.9454,52.5974,15247300
2004-08-25,52.5422,54.1672,52.1008,53.1641,9188600

我只想要股票数据,而不想要其他任何数据。所以我希望删除前 11 行。此外,将有几个文本文件用于不同的代码。所以str_replace 似乎不是一个可行的选择。我用来获取 CSV 文件并将所需内容放入文本文件的函数是

 function getCSVFile($url, $outputFile)
    {
        $content = file_get_contents($url);
        $content = str_replace("date,open,high,low,close,volume", "", $content);
        $content = trim($content);
        file_put_contents($outputFile, $content);
    }

我想要一个通用解决方案,它可以从 CSV 文件中删除前 11 行,并将剩余内容放入文本文件中。我该怎么做?

【问题讨论】:

  • file_put_contents(implode('', array_slice(file($url), 12)));
  • @IslamElshobokshy 前 11 行中的字符串将根据文本文件而变化。所以我认为这不是那个问题的重复。
  • ^ .. 但只需将其中一种方法扩展到 11。

标签: php csv file-get-contents php-7 file-handling


【解决方案1】:

这里的每个示例都不适用于大/巨大的文件。现在的人都不在乎记忆了。作为一名优秀的程序员,您希望您的代码高效且内存占用少。

改为逐行解析文件:

function saveStrippedCsvFile($inputFile, $outputFile, $lineCountToRemove)
{
    $inputHandle = fopen($inputFile, 'r');
    $outputHandle = fopen($outputFile, 'w');

    // make sure you handle errors as well
    // files may be unreadable, unwritable etc…

    $counter = 0;
    while (!feof($inputHandle)) {
        if ($counter < $lineCountToRemove) {
            fgets($inputHandle);
            ++$counter;
            continue;
        }

        fwrite($outputHandle, fgets($inputHandle) . PHP_EOL);
    }

    fclose($inputHandle);
    fclose($outputHandle);
}

【讨论】:

  • if 块中没有 fgets 调用吗?
  • 没有。当达到特定的计数器值时,fwrite 将开始工作。在此之前,将跳过每个 while 迭代。 Proof of concept
  • 但是,除非我没有理解问题,否则在继续之前没有fgets($inputHandle); 行,该函数将不会跳过输入文件的第一行。该脚本只会增加变量值而不移动文件指针。
  • 哈哈,你是对的。我完全错过了。我已经更新了我的问题。
【解决方案2】:

我有一个 CSV 文件,我希望删除其中的前 11 行。

我总是更喜欢使用explode 来做到这一点。

$string = file_get_contents($file);
$lines = explode('\n', $string);
for($i = 0; $i < 11; $i++) { //First key = 0 - 0,1,2,3,4,5,6,7,8,9,10 = 11 lines
    unset($lines[$i]);
}

这将删除它并使用implode 您可以从中创建一个新的“文件”

$new = implode('\n',$lines);

$new 将包含新文件

没有测试过,但我很确定这会起作用

小心!我会引用@emix 他的评论。

如果文件内容超过可用的 PHP 内存,这将失败。

确保文件不是“巨大”

【讨论】:

  • 这将删除 12 行。你想写$i &lt; 11 而不是$i &lt;= 11
  • 谢谢,我明白了。是一个错字:)
  • 如果文件内容超过了可用的 PHP 内存,这将失败。
【解决方案3】:

使用file() 将其读取为数组并简单地修剪前 11 行:

$content = file($url);

$newContent = array_slice($content, 12);

file_put_contents($outputFile, implode(PHP_EOL, $newContent));

但请回答以下问题:

  1. 为什么这个 CSV 文件中还有其他内容?
  2. 您如何知道要切断多少行?如果要剪切的行数超过 11 行怎么办?

【讨论】:

  • 我从 Macrotrends 下载了股票数据。所有文件的前 11 行都包含有关 Macrotrends 的一些额外信息。之后,就是我想要的股票数据了。
  • 如果文件内容超过了可用的 PHP 内存,这将失败。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-03
  • 2011-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多