I've tried to import csv file into mysql table and everything is Ok but I get a problem with Encoding the content of the file, the content is in "arabic Content."
The following image explains my problem:
1- this is my CSV FILE
2-My PHP code:
$c=1;
while($data=fgetcsv($file,1000,","))
{
if($c==1)
{
$c++;
continue;
}
$row=explode(";",$data[0]);
foreach($row as $val)
{
echo $val." ";
}
echo "<br>";
$c++;
}
3- The Result:
I try to insert data in mysql table
4- If I insert these data into Mysql Table, it will be :
I'm thankful for any help,
Thanks
You need to convert your CSV file in UTF-8 or process your string using utf8_encode.
Make sure your MySQL table is also in UTF-8
/open file pointer to standard output
$fp = fopen('php://output', 'w');
//add BOM to fix UTF-8 in Excel
fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
Thanks for all answers but the solution is:
open CVS file in any editor
re-save it with UTF-8 encoding and CVS extension
and upload it by an script
Related
I have a csv file that have data like this:
Sub District District
A Hi อาฮี Tha Li District ท่าลี่
A Phon อาโพน Buachet District บัวเชด
when I tried to read it using php code by following this SO question:
<?php
//set internal encoding to utf8
mb_internal_encoding('utf8');
$fileContent = file_get_contents('thai_unicode.csv');
//convert content from unicode to utf
$fileContentUtf = mb_convert_encoding($fileContent, 'utf8', 'unicode');
echo "parse utf8 string:\n";
var_dump(str_getcsv($fileContentUtf, ';'));
But it didn't work at all. Someone please let me know what I am doing wrong here.
Thanks in advance.
There are 2 issues with your code:
Your code applies str_getcsv to whole file contents (instead of individual line)
Your code example is using delimiter ";" but there is no such symbol in your input file.
Your data is in either fixed field length format (which is actually not a csv file) or in tab delimited csv file format.
If it is tab delimited file format then you can use 2 ways to read your file:
$lines = file('thai_unicode.csv');
foreach($lines as $line){
$data = str_getcsv($line,"\t");
echo "sub_district: ". $data[0].", district: ".$data[1]."\n";
}
or
$f = fopen('thai_unicode.csv',"r");
while($data = fgetcsv($f,0,"\t")){
echo "sub_district: ". $data[0].", district: ".$data[1]."\n";
}
fclose($f);
And in case you have fixed length fields data format you need to split each line yourself because csv related php function are not suitable for this purpose.
So you will end up with something like this:
$f = fopen('thai_unicode.csv',"r");
while($line = fgets($f)){
$sub_district = mb_substr($line,0,20);
$district = mb_substr($line,20);
echo "sub_district: $sub_district, district: $district\n";
}
fclose($f);
I have detect my value to write is Thai language and I will write to CSV file but when I use fwrite and save .CSV file and open this CSV file in Excel.I see my text about "เธฅเธนเธเธเธฅเธดเนเธ,เธชเธฃเนเธฒเธเธฅเธฒเธข" Below this code
$xfile =fopen($filename,"w");
foreach( $data as $itm ){
$outstr="";
foreach($itm as $key=>$str){
$val =str_replace("\r\n","",$str);
val =str_replace("\t\t","",$val);
$val =str_replace('"',"'",$val);
$outstr=$outstr.'"'.$val.'"'.$clm;
//dump(mb_detect_encoding($outstr));die(); --Result UTF-8
}
$outstr=substr($outstr,0,strlen($outstr)-1);
fwrite($xfile,$outstr."\r\n"); //Newline
fclose($xfile);
I feel stupid Or i forgot something. Please help
I use BOM with UTF-8 and insert after value. I see from this link
$xfile =fopen($filename,"w");
$BOM = "\xEF\xBB\xBF"; // UTF-8 BOM
fwrite($xfile, $BOM);
Try mb_convert_encoding before you write.
$xfile =fopen($filename,"w");
foreach( $data as $itm ){
$outstr="";
foreach($itm as $key=>$str){
$val =str_replace("\r\n","",$str);
val =str_replace("\t\t","",$val);
$val =str_replace('"',"'",$val);
$outstr=$outstr.'"'.$val.'"'.$clm;
//dump(mb_detect_encoding($outstr));die(); --Result UTF-8
}
$outstr=substr($outstr,0,strlen($outstr)-1);
$outstr= mb_convert_encoding($outstr, "UTF-8");
fwrite($xfile,$outstr."\r\n"); //Newline
fclose($xfile);
Check detail here PHP_mb_convert_encoding
I'm trying to import a Excel file to my PHP application with PHPExcel but It cannot work correctly with UTF-8 data.
This is my data
Độc quyền
and this is what I retrieve:
Äá»™c quyá»n
This is my code:
$cell = $objWorksheet->getCellByColumnAndRow($col, $row);
$value = $cell->getCalculatedValue();
$arraydata[$row-2][$col]=$value;
Thanks for your help. :)
I'm using this function to read rows from csv file.
<?PHP
$file_handle = fopen("test.csv", "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
echo $line_of_text[0] . $line_of_text[1]. $line_of_text[2] . "<BR>";
}
fclose($file_handle);
?>
And I'm getting this
PK!|l˜l [Content_Types].xml ¢( Ì”]KÃ0†ïÿCÉ4Ù&ˆÈº]øq©çˆÍé–&!'›Û¿÷4û#¤nzÓÐæœ÷}’4ïp¼jL¶„€ÚÙ‚õye`K§´ìmú”ß²£´Jg¡`k#6]^§k˜Q·Å‚Õ1ú;!°¬¡‘ÈK3•ŒôfÂËr.g ½Þ(`c[ 6>#%&f+ú¼! `e÷›ÂÖ«`Ò{£K‰Túæ’o8u¦¬µÇ+Â`¢Ó¡ùÙ`Û÷B[´‚l"C|– aˆ•.Ìß›óÃ"”®ªt Ê•‹†v€£ Ö±1<¼‘Úî¸ø§bi蟤]_>‘cðO8®ÿˆ#Òÿ"=$IæÈ`\À3¯v#z̹–Ôk”gøª}ˆƒîÑ$8”(Nß…]d´Ý¹'!QÃ>4º.ßÞ‘ÒètÃo·Ú¼S :¼EÊ×Ñ'ÿÿPK!µU0#õL_rels/.rels ¢(
Œ’ÏNÃ0ÆïH¼CäûênH¡¥»LH»!TÀ$îµ£$#÷ö„‚JcÛÑöçÏ?[ÞîæiTb/Nú(A±3b{×jxŸV b"giÇŽaWÝÞl_x¤”›b×û¨²‹‹º”ü#b4Oñìr¥‘0QÊahÑ“¨eÜ”å=†¿P-<ÕÁj{ª>ú<ù²·4Mox/æ}b—NŒ#ž;ËvåCf©ÏÛ¨šBËIƒóœÓÉû"cž&Ú\Oôÿ¶8q"K‰ÐHàó<ߊs#ëë.Ÿh©ø½Î<⧄áMdøaÁÅT_ÿÿPK!Þ ý(Ôxl/_rels/workbook.xml.rels ¢(
¼“ÏjÃ0ÆƒÑ}q’ne”:½ŒA¯[÷&QâÐÄ6–ö'o?“Cº#É.¡ƒ$ü}?Чýá§ïÄjU%)´¥«ZÛ(ø8½><ƒ Ö¶Ò³¨`#‚Cq·ÃNsüD¦õ$¢Š%†Ù襤Ò`¯)qmœÔ.ôšcéuyÖ Êõ’ýãšöO /îc)ÇwÚ‡œÝbñÿÿPK!«h&¨bqxl/workbook.xmlŒRËNÃ0¼#ñ–ï4©Ó·šTB€è!QÚ³‰7UÇŽl‡´ÏÆQKQ/œv×;ÏŒ³\+E¾Á:itJ‡ƒ˜Ð¹RïSú¹yy˜Qâ<ׂ+£!¥'pt•Ýß-[c_Æh—ÒÒûzE./¡ân`jи)Œ¸ÇÑî#W[à•¾R‹ãITq©iÏ°°ÿá0E!sx2ySö=‰Å=Êw¥¬Í–…T°í^×o¼BÝGE‰âÎ?éA¤tŒ£iáÏmêÇF*ÜΓ˜Ñ(»˜|·D#Áå7hïÌŽy±c“ÙE±•Ðºßº‘wRÓ¦t4ÃhOç)Á¡ ›¾L)›N稩?{¹/=Ò'É8îØ£+ú ^*ÑÁÝGê_ª«k4€½]HlìZ;†4»BcAß7èä
ýuŽ’r®rŒª+AÄhÓ8h{w†ýK’
ÇcÌxLÚÞœHïÚÖûï]Å›*$A°>–›¸í…J%›•ŠôaËËACMÒÛʈ÷¼ÆJꟉ&Mœ;žÖ4BÎe— tˆYÛ>c~4$”‡–
I have tried many other but getting the same output.
Is there any way I can get string?
Thanks in advance.
This is not a CSV file, it is a ZIP file (note the PK at the beginning).
Probably this is an xlsx file (excel in xml inside a zip package) that you either misnamed or that has the full name test.csv.xlsx but Windows is hiding your real extension (if you are on Windows).
Try to give lineseperator of your csv file like
$csvcontent = fgetcsv($handle, 1000,"\t");
where "\t" is your line seperator.and I think it is not an CSV file...either it is an Zipped file or like .xlsx extended file,First check it once
include_once 'mysqlconn.php';
include_once "functions.php";
$filename = $_GET['par'].".xls";
header("Content-type: application/x-msexcel");
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
if ($_GET['i'] == "par1") {
func1();
} else if ($_GET['i'] == "par2") {
echo "şşşıııİİİ";
func2();
} else if ($_GET['i'] == "par3") {
echo "şşşıııİİİ";
func3();
}
this is my export2excel.php file and func1,2,3 are in functions.php file and produces table output all work well except character encoding in a strange way. I am using utf-8 encoding for all my files. 2nd else if statement above produces healthy encoded output but rest 2 are encodes my output with strange characters like "BÃœTÇE İÇİ". it is "BÜTÇE İÇİ" in turkish.
in short. same files, same encoding, same database but different results.
any idea?
Excel uses UTF-16LE + BOM as default Unicode encoding.
So you have to convert your output to UTF-16LE and prepend the UTF-16LE-BOM "\xFF\xFE".
Some further information:
Microsoft Excel mangles Diacritics in .csv files?
Exporting data to CSV and Excel in your Rails apps
Instead I would use one of the existing libraries
PHP Excel Extension PECL extension by Ilia Alshanetsky (Core PHP Developer & Release Master)
Spreadsheet_Excel_Writer PEAR Package
PHPExcel
Edit:
Some code that could help if you really not want to use an existing library
<?php
$output = <<<EOT
<table>
<tr>
<td>Foo</td>
<td>IñtërnâtiônàlizætiøöäÄn</td>
</tr>
<tr>
<td>Bar</td>
<td>Перевод русского текста в транслит</td>
</tr>
</table>
EOT;
// Convert to UTF-16LE
$output = mb_convert_encoding($output, 'UTF-16LE', 'UTF-8');
// Prepend BOM
$output = "\xFF\xFE" . $output;
header('Pragma: public');
header("Content-type: application/x-msexcel");
header('Content-Disposition: attachment; filename="utf8_bom.xls"');
echo $output;
if anyone is trying to use the excel_writer in moodle and is getting encoding issues with output - say if you're developing a report that has a url as data in a field - then in this instance to simply fix this issue I wrapped the data in quotes so it at least opened up in excel here's my example:
// Moodles using the PEAR excel_writer export
$table->setup();
$ex=new table_excel_export_format($table);
$ex->start_document( {string} );
$ex->start_table( {string} );
// heading on the spreadsheet
$title = array('Report Title'=>'Report 1');
$ex->add_data($title);
// end heading
$ex->output_headers( array_keys($table->columns) );
**foreach($data as $row){
$string="'".trim($row->resname,"'")."'";
$row->resname=$string;
$ex->add_data( $table->get_row_from_keyed($row) );
}**
$ex->finish_table();
$ex->finish_document();
Excel uses UTF-16LE as the default encoding. So you should either convert UTF-8 to UTF-16LE yourself or use one of the tried and tested Excel PHP libs instead of trying to reinvent the wheel. I would recommend using PHPExcel...