-2

我有一个每天交付给我的 CSV 文件,最后与我不需要的数据合并,如下所示:

    "Date","Site","PCode","PName","Sales"
    "2013/03/10 00:00:00","10195","66011","GT 1","9"
    "2013/03/10 00:00:00","22413","66005","GT 2E","4"
    "2013/03/10 00:00:00","10082","66003","GT 23E","6"
    "SCode"
    "12345678"

我想在其上运行一个 PHP 脚本,删除包括“SCode”在内的所有字符。文件中的位置和文件总长度每天都在变化,因此它需要来自单词而不是来自设定位置。

有任何想法吗?我看过,但大多数示例在设置位置后删除,不会随不同文件更改。谢谢你。

像这样的新文件:

    "Date","Site","PCode","PName","Sales"
    "2013/03/10 00:00:00","10195","66011","GT 1","9"
    "2013/03/10 00:00:00","22413","66005","GT 2E","4"
    "2013/03/10 00:00:00","10082","66003","GT 23E","6"

从下面的反馈中使用了这个谢谢:

    <?
    $path="test.csv";
    $csv = file_get_contents($path);

    $new_csv = strstr($csv,'"SCode"',true); //csv file up to "SCode"

    $file = fopen("test2.csv","w");
    fwrite($file,$new_csv);
    fclose($file);
    ?>
4

1 回答 1

0

如果 csv 文件不是大得离谱,您可以使用该strstr()函数,并将标志$before_needle设置为 true:

$csv; //your loaded in csv file as a string

$new_csv = strstr($csv,'"SCode"',true); //csv file up to "SCode"
于 2013-03-11T07:07:05.947 回答