Jump to content

UTF-8 problem with Echo when reading from .csv file


foggykt

Recommended Posts

I have one problem...When i read from .csv file, i dont have Characters like Č,Ž,Š...Instead, its blank or some wierd characters...How to fix that? This is my code, everything works good, except for that :

 

<?PHP

$today = date("d"."."."m"."."."Y");     

 

$file_handle = 1;

$file_handle = fopen("1-ponedjeljak.csv", "r");

 

while (!feof($file_handle) ) {

 

$line_of_text = fgetcsv($file_handle, 1024);

 

 

echo "<tr>"; 

echo "<td width=70>".$line_of_text[1]."</td>";

 

$line_of_text[2] = mb_convert_encoding($line_of_text[2], 'UTF-8');  /*---> ive tried here but i doesnt work */

if($line_of_text[2]=="BONANZA"){

  echo  "<td width=700>"."<a href=\"bonanza.html?keepThis=true&TB_iframe=true&height=550&width=672\" class=\"thickbox\">BONANZA</a>"."</td>"; 

}

else {

    echo "<td width=700>".$line_of_text[2]." - ".$line_of_text[3]."</td>";

   

}

 

echo "<td width=90>".$line_of_text[4]."</td>";

echo "<td width=70>".$line_of_text[5]."</td>";

echo "</tr>"; 

 

 

 

 

}

 

 

fclose($file_handle);

 

?> 

 

 

Link to comment
Share on other sites

What is the encoding of the input file?  Your call to mb_convert_encoding() says to convert to utf-8, but not what to convert from

 

And if you are going to use utf-8 in your html, you should tell the browser to expect utf-8 : http://blogs.sitepoint.com/guide-web-character-encoding/

 

It's unfortunately a tricky issue to deal with properly.  The basic idea is that you can tell the browser what encoding to expect using "meta http-equiv", or using HTTP headers.  And you can use mb_convert_encoding() to convert from one encoding to another, but you need to tell it what encoding to convert from.  If you have no idea what encoding to convert from, try iso-8859-1.

Link to comment
Share on other sites

i tried ut8_decode but no luck. In my original .csv file all the letters are normal, i see ČŽŠ and all other lettters.

i have <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> set in my file.

i dunno, cant get it working, ive tried alot of combinations

Link to comment
Share on other sites

Hmmm, i use dreamweaver, and i put my php file to server and my web page shows results without thiese signs...Here you have EUVARI BAŠTINE, i need to have ČUVARI BAŠTINE. ive find some sort of solution, ive opened csv file with notepad->save as that same file, but the coding was ASCII so i changed it to UTF-8 and save. Now alomost everything works good, the only thing thats not good are strings that have first letter ČŽŠĆĐ, then the first letter is cut off

Link to comment
Share on other sites

Hmmm, in my csv file, that ive uploaded here, it is "ČUVARI" :S

In screenshot everything is ok.hmmm...well, ive managet to get it working, changed codingin csv file by opening it with notepad->save as-> and then just changed the coding to UTF-8. And then everything is ok, just words that have first letter as ĆČŠŽĐ dont show only the first letter. For example ČUVARI is echoed like UVARI, and then i just created if statement and if i have UVARI then i echo ČUVARI, and its ok.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.