473,509 Members | 2,526 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

UTF-8 file reading and writing for PHP

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

Jun 3 '06 #1
6 19953
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
Jun 3 '06 #2
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


Jun 3 '06 #3
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
Jun 4 '06 #4
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>

Save your (processing) PHP script in UTF-8.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jun 4 '06 #5
Thank you, both of you, for your help... I finally figured out what
does it:

The part I didn't mention (go figure, I thought it was harmless) is
that I was doing some str_replaces (with regex) on the UTF8 stuff, and
that is what messed up the output. So, when I accept the normal input,
save it to the file, and throw out the normal output, it comes out
exactly as I typed it (I'm testing with Arabic).

So I guess a followup question would be: How do I parse through UTF8
stuff with regexpressions? I'll do some research tomorrow.

Thank you again!

HaggMan
R. Rajesh Jeba Anbiah 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>

Save your (processing) PHP script in UTF-8.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/


Jun 6 '06 #6
HaggMan wrote:
<snip>
So I guess a followup question would be: How do I parse through UTF8
stuff with regexpressions? I'll do some research tomorrow.


You may use mb_ereg and any other mb string functions
<http://in2.php.net/mbstring>

And, may also use hexadecimal representation with PCRE functions such
as preg_match() <11**********************@o13g2000cwo.googlegroups .com>
( http://groups.google.com/group/comp....4b602f9b5a78b? ),
but it will be more cumbersome.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jun 6 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
4149
by: lawrence | last post by:
Someone on www.php.net suggested using a seems_utf8() method to test text for UTF-8 character encoding but didn't specify how to write such a method. Can anyone suggest a test that might work?...
38
5695
by: Haines Brown | last post by:
I'm having trouble finding the character entity for the French abbreviation for "number" (capital N followed by a small supercript o, period). My references are not listing it. Where would I...
32
49649
by: Wolfgang Draxinger | last post by:
I understand that it is perfectly possible to store UTF-8 strings in a std::string, however doing so can cause some implicaions. E.g. you can't count the amount of characters by length() | size()....
1
15547
by: stevelooking41 | last post by:
Can someone explain why I don't seem unable to use document.write to produce a valid UTF-8 none breaking space sequence (Hex: C2A0) ? I've tried everyway I've been able to find to tell the...
0
4220
by: Tim Northrup | last post by:
Help! We have DB2 V7.2 (fixpak 12) installed on Windows2003 Server, and the latest V7.2 client installed on another system. The DB2CODEPAGE on all systems is set to 1208, and the database was...
1
2002
by: David Bertoni | last post by:
Hi all, I'm trying to resolve what appears to me an inconsistency in the XML 1.0 recommendation involving entities encoding in UTF-16 and the requirement for a byte order mark. Section 4.3.3...
6
13875
by: archana | last post by:
Hi all, can someone tell me difference between unicode and utf 8 or utf 18 and which one is supporting more character set. whic i should use to support character ucs-2. I want to use ucs-2...
7
12077
by: Jimmy Shaw | last post by:
Hi everybody, Is there any SIMPLE way to convert from UTF-16 to UTF-32? I may be mixed up, but is it possible that all UTF-16 "code points" that are 16 bits long appear just the same in UTF-32,...
23
4988
by: Allan Ebdrup | last post by:
I hava an ajax web application where i hvae problems with UTF-8 encoding oc chineese chars. My Ajax webapplication runs in a HTML page that is UTF-8 Encoded. I copy and paste some chineese chars...
35
4301
by: Bjoern Hoehrmann | last post by:
Hi, For a free software project, I had to write a routine that, given a Unicode scalar value U+0000 - U+10FFFF, returns an integer that holds the UTF-8 encoded form of it, for example, U+00F6...
0
7233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7135
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7342
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7410
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7067
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
3215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
440
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.