note 50220 deleted from function.fgetcsv by nlopess
| From: | nlopess@php.net | Date: | Sun, 01 May 2005 08:24:22 +0000 |
| Subject: | note 50220 deleted from function.fgetcsv by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-88823@lists.php.net to get a copy of this message | ||
Note Submitter: bhawin13 at yahoo dot co dot in
----
If you use japanese data in csv file some field data contain japanese may not parsed properly
through fgetcsv.
Problem: starting character of field is not parsed properly (japanese field data).
solution:
This will attach double quote to all fields where it is require.
Asuming you are uploading csv file.
code:
==============================
/*
$_FILES['t_filename']['tmp_name'] is uploaded file.
*/
$arrLines = file($_FILES['t_filename']['tmp_name']);
for($intI=0; $intI<count($arrLines); $intI++) {
$arrFields = explode(",",trim($arrLines[$intI]));
$bnlQFlag = true;
for($intJ = 0; $intJ < count($arrFields); $intJ++) {
if(substr_count($arrFields[$intJ],'"' )%2 != 1 ) {
if($bnlQFlag){
$arrFields[$intJ] = '"' .$arrFields[$intJ].'"';
$bnlQFlag = true;
}
} else {
if($bnlQFlag == false) {
$bnlQFlag = true;
} else {
$bnlQFlag = false;
}
}
}
$arrLines[$intI] = implode(",",$arrFields );
}
$strFileContent = implode("\r\n", $arrLines);
$strFile=fopen($_FILES['t_filename']['tmp_name'],"w+");
//echo $strFileContent;
fwrite($strFile,$strFileContent);
fclose($strFile);
==============================