convert csv file from UTF-8 to ANSI encoding - php

I am getting data from a db table which was in array. So i am using the below code to write that data into csv . in the array we will have some data related to utf-8. But i need the file encoding type should be ANSI. Because the other side system will accept only ANSI encoding files. Below is my code.
$fh = fopen($new_file, 'w+');
header('Content-Encoding: Windows-1252');
header('Content-Type: text/csv; charset=Windows-1252');
foreach($input as $curl_response)
{
fputs($fh, implode($curl_response, ';')."\n");
}
// ... close the "file"...
fclose($fh);
After generating csv file when i open that in notepad and choose save as option it is showing me as UTF-8. Where i am doing wrong. Any help would be greatly appreciated.

Related

PHP read a line from a csv file return wrong in charset

I got a csv file, if I set the charset to ISO-8859-2(eastern europe) in Libre Calc, than it renders the characters correctly, but since the server's locale set to EN-UK.
I can not read the characters correctly, for example:
it returns : T�t insted of Tót.
I tried many things like:
echo (mb_detect_encoding("T�t","ISO-8859-2","UTF-8"));
I know probably the char does not exist in UTF-8 but I tried.
Also tried to setup the correct charset in the header:
header('Content-Type: text/html; charset=iso-8859-2');
echo "T�th";
but its returns : TÄĹźËth insted of Tóth.
Please help me solve this, thanks in advance
I advise against setting the header to charset=iso-8859-2'. It is usual to work with UTF-8. If the data is available with a different encoding, it should be converted to UTF-8 and then processed as CSV. The following example code could be kept as simple as the newline characters in UTF-8 and iso-8859-2 are the same.
$fileName = "yourpath/Iso8859_2.csv";
$fp = fopen($fileName,"r");
while($row = fgets($fp)){
$strUtf8 = mb_convert_encoding($row,'UTF-8','ISO-8859-2');
$arr = str_getcsv($strUtf8);
var_dump($arr);
}
fclose($fp);
The exact encoding of the CSV file must be known. mb_detect_encoding is not suitable for determining the encoding of a file.

UTF8 encoding difficulty using PHP in Notepad++

I have been making a program that creates multiple CSV's from another source CSV (encoded in 'SJIS'/SHIFT-JIS). Here's the process in which I am creating them:
Create a string, which will hold the contents of the output CSV's
Fill in said strings with their proper information
Encode the string to UTF-8 from SJIS using mb_convert_encoding()
code:
$contents2 = mb_convert_encoding($contents, "UTF-8", "SJIS");
Create a zip archive using PHP's provided library methods and append the files I desire with their corresponding strings using addFromString()
code:
$zipFileName = "output.zip";
$zip = new ZipArchive;
if ($zip->open($zipFileName, ZipArchive::CREATE) === TRUE){
$zip->addFromString('customer.csv', $contents2);
...do for the other files
$zip->close();
}
else{
echo 'Failed! File not created!';
}
Prompt the user with a dialogue box to save the file in their desired location.
code:
$zipContents = file_get_contents($zipFileName);
header('Content-Type: application/zip');
header("Content-Disposition: attachment; filename=inflow.zip");
header("Pragma: no-cache");
header("Expires: 0");
echo $zipContents;
Now here is my problem: The files that I have created from the zip file are encoded in "UTF-8 without BOM" when I open it in Notepad++. However, I require for these files to just be in plain "UTF-8". A inventory program I am using to upload these files, for reasons beyond me, will not show the proper characters for the CSV's encoded in "UTF-8 without BOM". Once I manually: open the files, re-encode it as "UTF-8", and save them, are the files able to display the correct characters in this inventory program.
I have read a good deal of articles talking about the converse of this problem, where people were seeking to make their UTF-8 files become encoded without BOM. However, my situation is the exact opposite of this. If there's an easy solution in PHP I would more than welcome the help! Thanks for reading!!
Found a possible solution, see this: How can I output a UTF-8 CSV in PHP that Excel will read properly?

PHP import CSV with utf-8 accents

I am having issues importing a CSV file which contains (french) names with accents in them... when ever they are imported the accent do not display properly example
félix turns into fŽlix
the file is created by hand and then imported into PHP.
I have tried both utf8_encode() and utf8_decode() and nether function will convert the chars so they can be viewed properly.
my question is how can i get this to render properly... convert char-set.. etc
I believe the text is encoded in Cp850 based on other questions i've seen on here. I am using fgetcvs() to get the contents.
Set Header Information before you output as UTF
header('Content-Type: text/html; charset=utf-8');
$log = file_get_contents("log.csv");
echo utf8_encode($log);
Output
félix
Please, try iconv() function
I think this is late answer but may be helpful for those who are still searching for solution. This is just a tweak. Not always recommended .
header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename=filename.csv');
echo "\xEF\xBB\xBF"; // UTF-8 with BOM
readfile("filename.csv");
exit;
I'm doing this on upload
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_dir .$target_file)) {
$log = file_get_contents($target_dir .$target_file);
file_put_contents($target_dir .$target_file, utf8_encode($log));

In PHP, when I use fwrite, I don't get the correct character set

Here is my code:
<?php
header("Content-Type: text/html; charset=UTF-8");
header("Content-Type: application/x-javascript; charset=UTF-8");
$fName = "demo.txt";
$str = "óé";
fid = fopen($fName, 'wb') or die("can't open file"); // Open file
fwrite($fid, $str); // Write to file
fclose($fid); // Close file
?>
To the screen, the output is:
óéü
When I open the file I get:
óéü
I am trying to save large amounts of data using fwrite, but the characters are not encoding correctly at the point of file save.
Thanks in advance.
fwrite stores strings binary. It does not do any charset conversion.
It's more likely that your PHP script is in a wrong charset, and thus the original "óéü" string. Show us the bin2hex($str) and bin2hex(file_get_contents('demo.txt')) if you can't debug it yourself.
There are some generic options to solve such problems:
Using utf8_encode($str) before saving.
Writing the UTF-8 BOM into the output file first fwrite($f, "\xEF\xBB\xBF")
correct conversion with iconv()
or adapting the php script itself with recode L1..UTF8 script.php
what program are you using to "open" the file? that program could be the problem.
First, insert the utf_encode inside the fwrite, like this:
<?php
$fName = "demo.txt";
$str = "óé";
fid = fopen($fName, 'wb') or die("can't open file"); // Open file
fwrite($fid, utf_encode($str)); // Write to file
fclose($fid); // Close file
?>
Next, remember to save your PHP script with UTF-8 without BOM encoding. Use any advanced code editor like Notepad++ to do this.

PHP fputcsv with UTF-8 Problem

I'm trying to allow my clients view some of the MySQL data in Excel. I have used PHP's fputcsv() function, like:
public function generate() {
setlocale(LC_ALL, 'ko_KR.UTF8');
$this->filename = date("YmdHis");
$create = $this->directory."Report".$this->filename.".csv";
$f = fopen("$create","w") or die("can't open file");
fwrite($f, "\xEF\xBB\xBF");
$i = 1;
$length = count($this->inputarray[0]);
fwrite($f, $this->headers."\n");
// print column titles
foreach($this->inputarray[0] as $key=>$value) {
$delimiter = ($i == $length) ? "\n\n" : ",";
fwrite($f, $key.$delimiter);
$i++;
}
// print actual rows
foreach($this->inputarray as $row) {
fputcsv($f, $row);
}
fclose($f);
}
My clients are Korean, and a good chunk of the MySQL database contains values in utf8_unicode_ci. By using the above function, I successfully generated a CSV file with correctly encoded data that opens fine in Excel on my machine (Win7 in English), but when I opened the file in Excel on the client computer (Win7 in Korean), the characters were broken again. I tried taking the header (\xEF\xBB\xBF) out, and commenting out the setlocale, to no avail.
Can you help me figure this out?
If, as you say, your CSV file has "correctly encoded data" - i.e. that it contains a valid UTF-8 byte stream, and assuming that the byte stream of the file on your client's site is the same (e.g. has not been corrupted in transit by a file transfer problem) then it sounds like the issue Excel on the client's machine not correctly interpreting the UTF-8. This might be because it's not supported or that some option needs to be selected when importing to indicate the encoding. As such, you might try producing your file in a different encoding (using mb_convert_encoding or iconv).
If you get your client to export a CSV containing Korean characters then you'll be able to take a look at that file and determine the encoding that is being produced. You should then try using that encoding.
Try encoding the data as UTF-16LE, and ensure that the file has the appropriate BOM.
Alternatively, send your clients an Excel file rather than a CSV, then the encoding shouldn't be a problem
Try wrapping the text in each fwrite call with utf8_encode.
Then use what is suggested here: http://www.php.net/manual/en/function.fwrite.php#69566

Categories