当前位置: 动力学知识库 > 问答 > 编程问答 >

php - fgetcsv with text-delimiter '

问题描述:

I am trying to upload the data of an csv file into my database.


The problem is that the excel file is far from 'normal':

The line end character is tabulator (\t)

and the text-delimiter is apastrophe (').


Now, I am trying to get the following function to work. With text-delimiter " it does work!

public function uploadExcel()

{

DB::table('members')->delete();

$filename = Input::file('import_file')->getRealPath();

$file = fopen($filename, "r");

$count = 0;

while (($emapDataUnescaped = fgetcsv($file, 10000, "\t")) !== FALSE) {

$emapData = self::addslashes_recursive($emapDataUnescaped);

$count++;

if($count>1) {

DB::statement("INSERT INTO `members` (

member_title,

member_first_name,

member_name_affix,

member_last_name,

member_private_address,

member_private_zip_code,

member_private_location,

member_private_phone,

member_private_mobile,

member_private_fax,

member_private_mail,

member_business_position,

member_business_name,

member_business_address,

member_business_zip_code,

member_business_location,

member_business_area_code,

member_business_phone,

member_business_fax,

member_business_mobile,

member_business_mail,

member_join_date,

extra

) VALUES (

'$emapData[0]',

'$emapData[1]',

'$emapData[2]',

'$emapData[3]',

'$emapData[4]',

'$emapData[5]',

'$emapData[6]',

'$emapData[7]',

'$emapData[8]',

'$emapData[9]',

'$emapData[10]',

'$emapData[11]',

'$emapData[12]',

'$emapData[13]',

'$emapData[14]',

'$emapData[15]',

'$emapData[16]',

'$emapData[17]',

'$emapData[18]',

'$emapData[19]',

'$emapData[20]',

'$emapData[21]',

'$emapData[22]'

)");

}

}

return redirect('index');

}


Add slashes_recursive function

public function addslashes_recursive($data)

{

if (is_array($data)) {

return array_map('addslashes', $data);

} else {

return addslashes($data);

}

}



I tried all kind of quotations marks etc. but I just cannot figure it out! I would be very thankful for any kind of help!



UPDATE

The file is not just on one line. Here are the options to display it correctly using LibreOffice:


分享给朋友:
您可能感兴趣的文章:
随机阅读: