假設您可以將您的代碼轉換爲文本字符串,您可以將其替換/刪除然後解析文本並將其轉換爲支持fputcsv()的格式。
寫入文件夾時,請務必檢查您的用戶是否有權寫入此文件夾(例如,如果父文件夾的權限爲drwxr-xr-x,則可以使用chmod g + w folder_name修復它) 。
<?php
// sample text to parse
$some_text = '"DENO;NAME;SURNAME;""BIRTH"";""ZIP"";CITY;E-MAIL;TELEPHONE"
"M;DAVID;BON;""1959-02-12 00:00:00"";75009;PARIS;[email protected];010000000"
"M;DOE;JHON;""1947-02-02 00:00:00"";75008;PARIS;[email protected];060000000"
"M;DAVE;Philippe;""1950-01-01 00:00:00"";75002;""PARIS"";[email protected];070000000"';
// remove quotes
$final_text = str_replace('"', '', $some_text);
// create array of rows by searching for new lines
$data = str_getcsv($final_text, "\n");
// create an empty array to save our final csv data
$final_csv = array();
// loop thru the array and save to the final csv data
foreach ($data as $value) {
// before saving to final csv data, split row into individual column items
$value_array = explode(";", $value);
$final_csv[] = $value_array;
}
// helper debugger to show data before writing it
echo "<pre>";
print_r($final_csv);
echo "</pre>";
// create a new file for writing or open and truncate to 0
$fp = fopen('file.csv', 'w');
// write to file from final csv data
foreach ($final_csv as $fields) {
fputcsv($fp, $fields);
}
// close file
fclose($fp);
?>
看起來像你可能只是刪除所有的報價和使用semicol分裂 – 2015-10-14 00:40:45
我這樣做:(!FEOF($ file_handle)),而{$ = line_of_text fgetcsv($ file_handle,1024); }它不工作,就像我只有我列,如果我手動刪除報價之前導入它的工作。 – Nathan
你如何刪除引號? – 2015-10-14 00:48:52