2012-03-02 80 views
0

我想通過php處理一個CSV文件,並且讓它工作。但是有一個數組需要根據一組條件進行更改。if elseif indie while loop

$file_handle = fopen($path, "r"); 
while (!feof($file_handle)) { 
    $line_of_text = fgetcsv($file_handle, 100000); 
    if($currency == "US"){ 
     $line_of_text[6] = str_replace ("if_you_find_this","change_to_this",$line_of_text[6]); 
     $line_of_text[6] = str_replace ("if_you_find_this","change_to_this",$line_of_text[6]); 
    } elseif($currency == "DE"){ 
     $line_of_text[6] = str_replace ("if_you_find_this","change_to_this",$line_of_text[6]); 
     $line_of_text[6] = str_replace ("if_you_find_this","change_to_this",$line_of_text[6]); 
}else { 
     echo "Something with currency handling went wrong. Please contact support."; 
} 
    $data .= $line_of_text[0] . "," . $line_of_text[1] . "," . $line_of_text[2] . "," . $line_of_text[4] . "," . $line_of_text[6] . "," . $line_of_text[49] . "," . $line_of_text[51] . "\n"; 

} 
fclose($file_handle); 

$new_file_handle = fopen($path, "w"); 
fwrite($new_file_handle, $data); 

它沒有拋出任何錯誤,但似乎整個條件塊被忽略。幫幫我?

+0

忘記粘貼在已經在工作文件中的重要行 - > $ line_of_text = fgetcsv($ file_handle,100000); – jeffimperial 2012-03-02 05:56:58

回答

0

你沒有閱讀$file_handle任何地方。 feof($file_handle)永遠不會是真的。發佈後,此代碼應該永久循環。

(順便說一句,使用feof像這通常是你並不怎麼想這樣做,因爲這個原因等等,更好的將是這樣while (($line = however_you_read($file_handle)) !== FALSE)。)幾乎所有的流讀取功能對任何錯誤返回FALSE,或)

0

檢查文件是否正確打開或否。 在try catch中編寫代碼,因此如果出現任何錯誤,您可以找到它。 我認爲你的文件不包含任何內容,或者它沒有成功打開。