2013-03-11 56 views
-2

我有一個是交給我一個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

顯示一些代碼,你嘗試過或卡在..! – 2013-03-11 07:01:55

回答

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" 
+0

謝謝,這是我用過的: – CHinAU 2013-03-11 08:00:07

+0

<? $ path =「test.csv」; $ csv = file_get_contents($ path); $ new_csv = strstr($ csv,'「SCode」',true); // csv文件最大爲「SCode」 $ file = fopen(「test2.csv」,「w」); fwrite($ file,$ new_csv); fclose($ file); ?> – CHinAU 2013-03-11 08:00:28