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 HaggMan on 10/17/67 11:49

Thanks for the reply...

My goal is to allow user input in UTF-8, in Arabic script, for example.
I then save what they input to a file. Then I'd like to retrieve and
print out the original stuff that they wrote.

I've tried various variations of utf8_encode() and utf8_decode() and
even without them, and every time, the resulting stuff is just ????? or
other weird characters.

Bent Stigsen wrote:
> 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

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