473,836 Members | 1,472 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.ph p">
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($th efile); //To get rid of the BOM
$ret = "";
foreach ($thefile as $i => $line) {
$line = rtrim(utf8_deco de($line));
$ret .= $line;
}
echo $ret; // This _doesn't_ display the correct stuff

Jun 3 '06 #1
6 20051
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($th efile); //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_deco de($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_conten ts and file_get_conten ts.
/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($th efile); //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_deco de($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_conten ts and file_get_conten ts.
/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_enco ding($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************ **********@o13g 2000cwo.googleg roups.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
4195
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? Something that maybe gives 90% confidence that a given block of text is or is not UTF-8 encoded?
38
5754
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 find an answer to this question (don't find it in the W3C_char_entities document). -- Haines Brown brownh@hartford-hwp.com
32
49739
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(). Instead one has to iterate through the string, parse all UTF-8 multibytes and count each multibyte as one character. To address this problem the GTKmm bindings for the GTK+ toolkit have implemented a own string class Glib::ustring...
1
15623
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 browser I'm trying to print UTF-8 and still no luck. I'd like the first 2 tries to match the second two tries as far as output. <HTML> <meta http-equiv="Content-Type" content="application/x-script; charset=UTF-8">
0
4252
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 created with code set UTF-8 / codepage 1208. (Note: Running our test application described below on the database host as opposed to a separate client system produced the same results as described below). When we perform an INSERT statement...
1
2022
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 has the following text: http://www.w3.org/TR/REC-xml/#charencoding
6
13897
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 character in streamreader and streamwriter. How unicode and utf chacters are stored.
7
12163
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, but with zero padding and hence no real conversion is necessary? If I am completely wrong and some intricate conversion operation needs to take place, can anyone give me some primer on the subject?
23
5036
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 from another HTML page viewed in IE7, that is also UTF-8 encoded (search for "china" on google.com). I paste the chineese chars into a content editable div. My Ajax webservice compiles an XML where the data from the content editable div is...
35
4366
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 becomes 0x0000C3B6. I came up with the following. I am looking for a more elegant solution, that is, roughly, faster, shorter, more readable, ... while producing the same ouput for the cited range. unsigned int
0
9816
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9668
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10546
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9371
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6978
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5647
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4448
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 we have to send another system
2
4013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3112
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.