删除csv文件中某个位置后的所有字符

时间:2013-03-11 07:00:56

标签: php csv

我有一个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);
    ?>

1 个答案:

答案 0 :(得分: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"