I want to export some of my data into CSV with the help of PHP. The code is working correctly but some of the keywords that I want to export was in some other unicode which I had saved in database using
urlencode('אריה דרעי');
This saved the unicode in this format in database:
%26%231488%3B%26%231512%3B%26%231497%3B%26%231492%3B+%26%231491%3B%26%231512%3B%26%231506%3B%26%231497%3B
The main problem here is when I display it in an HTML page it displays fine but when I try to export it in CSV it shows the same texts.
I tried to use following function
url_decode('%26%231488%3B%26%231512%3B%26%231497%3B%26%231492%3B+%26%231491%3B%26%231512%3B%26%231506%3B%26%231497%3B');
But it again genereated special characters as
אריה דרעי
Then I tried it to further decode using
htmlspecialchars_decode();
But still it shows אריה דרעי in the CSV files.
I hope I make sense.
Try using htmlspecialchars() instead of url_encode()
http://www.php.net/manual/en/function.htmlspecialchars.php
and http://www.php.net/manual/en/function.htmlspecialchars-decode.php
Related
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
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.
I'm trying to display strings of text fetched from a database in a PDF document correctly. What I can't figure out is the following.
I'm using fpdf and html2pdf for the generation of the PDF document. After I fetched my information from my DB I use:
iconv('UTF-8', 'windows-1252', $data);
This displays correctly in the PDF document if I use:
$pdf->Cell();
But when I use:
$pdf->WriteHtmlCell();
it seems that it has decoding issues. It seems to be in another charset because ù turns into ù and Ä into Ä and so on. I have tried to convert it to UTF-8 (which it is originally in) or ISO, but I keep getting the same result. When I run a
mb_detect_encoding();
on the string it always comes back as ASCII (that is UTF-8?).
Is WriteHtmlCell(); using another encoding?
try this
html_entity_decode($your_data, ENT_XHTML,"ISO-8859-1");
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
Hi all I need your help for my problem.
I try to display text (Korean) from a .txt file but the output is different.
I have a .txt file contains Korean characters like this
냐는 한국을 사랑
but when i try :
$str= file_get_contents($path."result.txt");
echo $str;
on the browser the result came out like this : �먮뒗 �쒓뎅�� �щ옉
but It's OK when i just echo "냐는 한국을 사랑"
IS there something wrong ?
Thank for your help
Either use header("Content-Type: text/html; charset=UTF-8") in your php file or a meta tag in your html <meta charset='utf-8'>. And make sure the font you are using supports unicode characters you need.
Apparently the character encoding of the file is different from the character encoding of the HTML document that your code is generating.
You could dynamically convert the text data in PHP, or you could just use a suitable conversion program to convert the text file. You could just open the text file in a text editor and use Save As to save it as UTF-8 encoded (without BOM), assuming that your PHP is generating a UTF-8 encoded document.
I struggled a while fixing this problem until I discovered this which works perfectly for me:
echo call_user_func_array('mb_convert_encoding', array("행동 방식",'HTML-ENTITIES','UTF-8'));