PHP fputcsv does not display Chinese Character Correctly - php

I need your help to finish my project. I take the data from my json files, some of which consist of chinese characters, but when I try to write to .csv it does not display properly.
This is my code
function writeCsv()
{
$resource = fopen('c:/xampp/test.json','w');
$csvBodyData = [ 'item'=> '逆始感録機政'];
fputcsv($resource, $csvBodyData);
}
I have tried the following solution but it's still not working.
write utf-8 characters to file with fputcsv in php
I got this character "???".

In your case the problem was not in PHP. When you open a csv file in Excel it shows you a window, where you can setup CSV importing options like delimiter and encoding. You should choose UTF-8 encoding to view those Chinese characters.

Related

How do I to export utf8mb4 mysql data using php to a csv file

I am looking for help on a csv file export.
I have a mysql database encoded as utf8mb4 (unicode_ci) with a table using collation utf8mb4_unicode_ci for my fields. The data contains special characters such as copyright symbols, foreign characters such as "é", etc. I am trying to export data to a csv file but the string values that contain special characters are not translating over properly. For example, the copyright symbol comes up as "¬Æ" in the csv file I generate.
My environment is Laravel 7, PHP 7 and MySQL 5.7 on Ubunutu 18.0.4. My database connection is already setup as charset = "utf8mb4" and collation = "utf8mb4_unicode_ci" in my Laravel database config file. The meta tag in my page header is already set to use charset=utf-8 and the header used to generate the csv file is set to:
header('Content-Type: text/csv; charset=utf-8');
I have tried using:
iconv("utf-8", "ascii//TRANSLIT//IGNORE", $mystring);
but this only replaces some of the values with ascii representations and not the proper symbols. I have also tried using something like
htmlspecialchars($mystring, ENT_QUOTES, "UTF-8");
but this still returns "®" for the copyright symbol and other strange character sequences in the csv file. When I echo the values in php, they appear correctly on my page. Am I right in thinking that I need to somehow convert the utf8mb4 string to regular utf-8 when I append the row to my csv file? I have not been able to find a solution and am looking for some help.
Can anyone tell me what I need to do to get the expected symbols in my csv file?
Jerry's comment
You don't show the code you use to actually write the file. Also, you don't say how you're inspecting the result (if you are using Excel, that could be the problem).
and Sammitch's comment
It's not that the data is not exporting properly, it's that the program that is reading or displaying it is not using the correct charset. You can try adding a UTF8 BOM \xEF\xBB\xBF to the beginning of the file and the program may use that as a signal to apply the correct charset. Failing that, look up how to open UTF8 CSVs properly in that program. Failing that you'll need to translate the data to a charset that the program does handle correctly.
were helpful. I was using Excel to preview the file. When I looked at the raw csv data in a code editor, the expected characters are there so it is something with the way Excel handles the file. Since I am working on a Mac and the © symbol is being entered with [Option] + [G], the é is [Option] + [E], etc. it would make sense that it could be a translation problem with how Excel reads the file. Adding \xEF\xBB\xBF to the beginning of the file seems to have done the trick!
If you stored utf8 values into a column declared latin1, fix that first.
Do not use any conversion routines.
Do verify the data in the tables using SELECT(hex) and SHOW CREATE TABLE
More: Trouble with UTF-8 characters; what I see is not what I stored

Convert txt file encoding from DOS737 to UTF8

I have a txt file that has greek characters. When i open the file with notepad it shows that the encoding is ASCII.
But the only way that i can read the greek characters is to change (in openoffice writer or Editpad lite) the character set to DOS737.
The process that i need to implement in PHP is to open the file, split the text and import it to database. Everything is ok except that i cannot get the greek characters as they are.
I tried iconv but with no result.
I also tried mb_convert_encoding($data[0], "DOS737"); but i get warning mb_convert_encoding(): Unknown encoding "DOS737"
Also tried utf8_encode but with no luck
Any suggestions?
Finally found it.
It was easy... For anyone that might have the same issue use iconv("cp737","UTF-8","$string");

Why fgetcsv adds some characters between characters?

I use a php script with fgetcsv() to import data from csv files I did not create (no choice).
My problem is the data are imported to my database with a special character between each character, on each field...
For example, "Mounted print" is imported as "�M�o�u�n�t�e�d� �P�r�i�n�t�"
I tried to modify encoding with no result : utf8_encode/decode, iconv ...
Any idea ?
Thanks

PHP French and Russian characters after csv upload

I've created a csv import allowing users to upload their csv full of info on a Mysql database and displaying them on another webpage.
Now, some users are french and some others russian. So I'd need to be able to handle both sets of characters, but I find myself having problems with both.
I tried to add the utf8_decode tags before the variables but the situation doesn't change.
I'd like to know wether there is a general solution allowing to deal with both sets of characters in the same page??
ps in a previous page I was dealing with I handled it by passing the utf8_decode tag everytime I was dealing with a French variable, and by putting nothing everytime dealing with a russian variable. But in this case the trick doesn't work.
Thanks in advance. "the world of characters sets is a weird beist..."
marko.c
You could convert everything to UTF-32 just to be sure, you could try something like:
if(!mb_detect_encoding($csv, 'UTF-32', true)){
iconv(mb_detect_encoding($csv, mb_detect_order(), true), "UTF-32", $csv);
}
Ok so in the end the whole problem was in the csv upload. once added the following line to the csv upload
mysql_query("SET NAMES 'UTF8'");
everything worked properly. No need to recall any type of encoding nor decoding UTF8, both russian and french characters simpley work well.
cheers, thanks

Using PHP to write a CSV file with Spanish characters

I have a php script with utf-8 encoding. In it I have an array with special characters (like and n with a ~ on top). It looks just fine in my editor. The php matches the array with text coming in from a html form and writes a csv file. When I write the file I do it like this;
fwrite($fp,utf8_encode($data),strlen($data)+100);
When I open the file it says it is utf-8 encoded but the charters are all messed up.
have you tried without using utf_encode() on the data?
it seems that you are reencoding something that's already utf-8 encoded

Categories