 |
|
 |
|
Next: MySQL_connect() working in PHP4 not in PHP5?
|
| Author |
Message |
External

Since: Oct 17, 2006 Posts: 3
|
(Msg. 1) Posted: Sat Jun 03, 2006 11:59 am
Post subject: UTF-8 file reading and writing for PHP Archived from groups: comp>lang>php (more info?)
|
|
|
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.
HTML --------------- Form
<FORM name=saveform method=post action="wiki.php">
File:
<TEXTAREA name=thetext rows=20 cols=30></TEXTAREA>
</TEXTAREA>
<INPUT type=submit>
<INPUT type=hidden name=action value="save">
</FORM>
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));
fputs($fr, $message);
fclose($fr);
PHP --------------- Read the data from the file
$thefile = file($filename);
array_shift($thefile); //To get rid of the BOM
$ret = "";
foreach ($thefile as $i => $line) {
$line = rtrim(utf8_decode($line));
$ret .= $line;
}
echo $ret; // This _doesn't_ display the correct stuff >> Stay informed about: UTF-8 file reading and writing for PHP |
|
| Back to top |
|
 |  |
External

Since: Oct 17, 2006 Posts: 3
|
(Msg. 2) Posted: Sat Jun 03, 2006 4:02 pm
Post subject: Re: UTF-8 file reading and writing for PHP [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
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 >> Stay informed about: UTF-8 file reading and writing for PHP |
|
| Back to top |
|
 |  |
External

Since: Jun 25, 2004 Posts: 14
|
(Msg. 3) Posted: Sun Jun 04, 2006 12:30 am
Post subject: Re: UTF-8 file reading and writing for PHP [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
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 >> Stay informed about: UTF-8 file reading and writing for PHP |
|
| Back to top |
|
 |  |
External

Since: Jun 25, 2004 Posts: 14
|
(Msg. 4) Posted: Sun Jun 04, 2006 3:11 am
Post subject: Re: UTF-8 file reading and writing for PHP [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
HaggMan wrote:
> 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.
[snip]
If what you get sent is in UTF-8 and the page you send out is in
UTF-8, then you don't need to do anything.
Make sure what you get really is UTF-8 (e.g.
"mb_detect_encoding($thetext)" )
Also make sure the browser is told it is UTF-8. (check headers,
metatags, xml-declaration)
/Bent >> Stay informed about: UTF-8 file reading and writing for PHP |
|
| Back to top |
|
 |  |
| Related Topics: | PHP instances writing to the same file? - I read in Programming PHP (O'Reilly) that flock() "cannot prevent two PHP scripts running in the same web server process from accessing a file at the same time". In my case a single PHP script appends text strings to an existing text file. Sev...
Writing to a Local File - We have a PHP/MySQL database application. A user would like to be able to run a routine to read selected data from the database and export a flat file to their local C: drive. After reviewing the documentation and posts, it is not clear how to do this,...
writing to a txt file from a form - Hi all, i am having trouble trying to write a script to a txt file. I am attempting to seperate each posted item with the deliminater '|', but for some reason i am getting the following information written: peri|jones||||Submit Whereas the desired..
Writing results to file. - I would like to take this output and write it to a file, thankyou.html. Any help would be great. thanks, --tj if($gen_thank == "on" ) { $form_process .="?>\n\n\n<!-- This is the content of the Thank you page, be careful while cha...
Permission Denied when writing to a file - Whenever i try to write to a file i get: Warning: fopen(dateLogFile.txt): failed to open stream: Permission denied in **********.com/php/dateLog.php on line 10 |
|
You can post new topics in this forum You can reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|
 |
|
|