You are here: Re: UTF-8 file reading and writing for PHP « PHP Programming Language « IT news, forums, messages
Re: UTF-8 file reading and writing for PHP

Posted by Bent Stigsen on 10/02/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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация