note 50220 added to function.fgetcsv
| From: | bhawin13 at yahoo dot co dot in | Date: | Tue, 22 Feb 2005 06:04:42 +0000 |
| Subject: | note 50220 added to function.fgetcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-85351@lists.php.net to get a copy of this message | ||
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);
==============================
----
Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50220
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50220&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50220&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50220&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50220&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50220&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50220&report=yes
Search -- http://master.php.net/manage/user-notes.php