|
Posted by Bent Stigsen on 12/18/09 11:49
HaggMan wrote:
> I'm creating a page that:
> - accepts user input in whatever language
> - saves that input to a file
> - reads the file and displays the original input
>
> The following code successfully writes the user input to a file (when I
> open the file, it's in the correct font), but I can't get PHP to read
> the file and display the correct characters.
[snip]
> PHP --------------- Sticks the data in a file
> $message = $_REQUEST['thetext'];
> echo $message; // This displays the correct stuff
> $filename = "tmp/tmp.txt";
> $fr = fopen($filename, "wb+");
> // adding header
> fwrite($fr, pack("CCC",0xef,0xbb,0xbf));
Is it safe to assume the data to be UTF-8?
If you just discard the byteordermark later, there's little reason to
add it (if there ever was).
> fputs($fr, $message);
> fclose($fr);
>
> PHP --------------- Read the data from the file
> $thefile = file($filename);
> array_shift($thefile); //To get rid of the BOM
BOM = 3 bytes
$thefile = array of lines of text terminated by newline.
> $ret = "";
> foreach ($thefile as $i => $line) {
> $line = rtrim(utf8_decode($line));
I am not sure what to make of this. If you expect the browser to send
data in utf-8, then I would assume you serve your pages in utf-8, then
why convert the text to iso8859-1?
> $ret .= $line;
> }
> echo $ret; // This _doesn't_ display the correct stuff
Start with simple file_put_contents and file_get_contents.
/Bent
Navigation:
[Reply to this message]
|