php fgetcsv 没有环绕符,PHP fgetcsv()没有读取第一个UTF-8字符 - 使用fgets的alternatif...

我通过使用fopen和fgets而不是fgetcsv()打开文件并使用utf8_encode为每一行编写一个副本来解决这个问题 . 然后我使用副本并通过fgetcsv()将其放入

这是我更新的代码 .

function runCSVtoArray() {

// --> FOR IMPORT

//Function that converts a CSV file to a PHP array.

//echo 'Chargement du fichier CSV pour importation MYSQL....

';

$readCharsPerLine = (JRequest::getVar('charsPerLine') > 0) ? JRequest::getVar('charsPerLine') : 1500; /* Import as of 2012-04-16 seem to have max 800chars per line. 1500 is alot of extra. */

putenv("LANG=fr_CA.UTF-8");

setlocale(LC_ALL, 'fr_CA.UTF-8');

//ini_set("auto_detect_line_endings", true);

//iconv_set_encoding("internal_encoding", "UTF-8");

$openfile = $this->imp['importPath'].$this->imp['csvFileName'];

$utf8File = str_replace('.csv', '_utf8.csv', $openfile);

if ( file_exists($openfile) ) {

//echo 'Fichier CSV trouvé....

';

//rewrite the file in UTF8

if (JRequest::getVar('encodeutf8')) {

if (($handle = fopen($openfile, "r")) !== FALSE) {

$newFileHandle = fopen($utf8File, 'w'); //NEW UTF8 FORMAT

//fwrite($newFileHandle, "\xEF\xBB\xBF");

while (($the_line = fgets($handle)) !== FALSE) {

fwrite($newFileHandle, utf8_encode($the_line));

} //End of while()

}

$openfile = $utf8File;

}

//echo 'Ouverture du fichier : '.$openfile.'

';

if (($handle = fopen($openfile, "r")) !== FALSE) {

//echo 'Fichier CSV ouvert... Chargement en cours....

';

$row_i=0;

$this->_importData = array();

while (($data = fgetcsv($handle, $readCharsPerLine, ";")) !== FALSE) {

/*while (($the_line = fgets($handle)) !== FALSE) {*/

//$data = explode(';', $the_line);

//$debugoutput = implode('; ', $data); echo ( (JRequest::getVar('encodeutf8')) && ( mb_detect_encoding($debugoutput, "UTF-8") == "UTF-8") ) ? utf8_encode($debugoutput).'

' : $debugoutput.'

'; //Debug2

//$debugoutput = implode('; ', $data); echo $debugoutput.'

'; //Debug2

$num = count($data);

if ($row_i==0) {

// TITLE ROW

$keyRow = array();

$maxItems = count($data); //Count the number of ";"

for ($c=0; $c < $num; $c++) {

//Making title array with CSV first line

//Key for colum

if ( (JRequest::getVar('encodeutf8')) && ( mb_detect_encoding($data[$c], "UTF-8") == "UTF-8") ) {

//$data[$c] = utf8_encode($data[$c]);

$data[$c] = $data[$c];

}

if ($data[$c]!="") {

$keyRow[$c]=trim($data[$c]);

$keyRow[$c]=str_replace('GDWACCENT', '', $keyRow[$c]); //STRIP GDWACCENT, GDW uTF8 fgetcsv fix

}

else { $keyRow[$c]=''; }

}

} else {

//VALUE ROW...

for ($c=0; $c < $num; $c++) {

$key = $keyRow[$c];

if ( (JRequest::getVar('encodeutf8')) && ( mb_detect_encoding($data[$c], "UTF-8") == "UTF-8") ) {

//$data[$c] = utf8_encode($data[$c]);

$data[$c] = $data[$c];

$data[$c]=str_replace('GDWACCENT', '', $data[$c]); //STRIP GDWACCENT, GDW uTF8 fgetcsv fix

}

if ($data[$c]!="") {

$this->_importData[$row_i][$key]=trim($data[$c]);

$this->_importData[$row_i][$key]=str_replace('GDWACCENT', '', $this->_importData[$row_i][$key]); //STRIP GDWACCENT, GDW uTF8 fgetcsv fix

}

} //End of for()

}

$row_i++;

} //End while()

//echo 'HERE

';

//gdwprint($this->_importData);

//exit();

//echo 'Chargement terminer.... Sauvegarde en cours...

';

return true;

} else {

//Incapable d'ouvrir le fichier d'importation.

return false;

}

} else {

//FILE NOT FOUND...

return false;

}

} // runCSVtoArray()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值