Log in

View Full Version : text field read file need help with encode UTF-8



Tim Dobson
07-03-2010, 03:23 PM
I have posted this on another forum to try and get help but was unsuccessful.

My text field reads a text file but the text file contains UTF-8 and when the text field reads it i get all jibberish text. E.G. -Ŧķ-κıℓℓεя comes out as -Ŧķ-κıℓℓεÑ

I did try something like the following.


<?php
$myFile = "includes/docs/sdata.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($theData));
fclose($fh);
function fixEncoding($theData)
{
$cur_encoding = mb_detect_encoding($theData) ;
if($cur_encoding == "UTF-8" && mb_check_encoding($theData,"UTF-8"))
return $theData;
else
return utf8_encode($theData);
} // fixEncoding
echo '<span id="sprytextfield1">
<label for="templateedit">Edit template script</label><br /><textarea name="templateedit" cols="90" rows="90" id="templateedit">' . $theData . '</textarea>
<span class="textfieldRequiredMsg">A value is required.</span></span></div>'
?>

But clearly that is wrong or something in there is wrong :(

I am newish to php so you have to bare with my n00bish code.

Help would be greatly appriciated :)

djr33
07-03-2010, 06:52 PM
This isn't a complete answer yet, but there are many possible problems with encoding. Make sure that ALL text is always stored and displayed as UTF8. For example, there must be a meta tag on the page that does this, in addition to the format of the text file. A database might also cause more compatibility issues. Once all of this is correct, it'll be a lot easier to fix. What you have above may work fine, but that only works if you've correctly diagnosed where the problem is and unfortunately in encoding problems it often is in an unexpected place.

Tim Dobson
07-03-2010, 06:59 PM
This isn't a complete answer yet, but there are many possible problems with encoding. Make sure that ALL text is always stored and displayed as UTF8. For example, there must be a meta tag on the page that does this, in addition to the format of the text file. A database might also cause more compatibility issues. Once all of this is correct, it'll be a lot easier to fix. What you have above may work fine, but that only works if you've correctly diagnosed where the problem is and unfortunately in encoding problems it often is in an unexpected place.

ACHHHHHHHHHH meta tag!!! lol thanks it completely flew right over my head. Added the meta tag and woop it come out corretly. You i see what you mean by being "unexpected" i must of missed the meta tag in the file somewhere :rolleyes: