Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing special characters (chinesse) in a .txt

Status
Not open for further replies.

alexxxis

Programmer
Sep 21, 2006
38
0
0
RO
Hi !
I`m working on a small application to translate given pieces of text into different languages. Including chinesse, for example. I got the inserting into the sql database and reading/displaying correctly from the database parts right. Now ... i`m trying to write the pieces of text into a txt file. And i can`t manage to ... I tried utf8_encode on the string from the database, i still get the "amp;#26519;先生惘惘&" stuff IN the file i create ... and it`s driving me crazy. Please help.
 
have you looked at iconv? it has a translit switch that i have found very useful.
 
i`m ... lost

$stringtowrite="间";
//$stringtowrite = mb_convert_encoding( $stringtowrite, 'UTF-16LE', 'UTF-8');
//$stringtowrite=html_entity_decode(htmlspecialchars_decode($stringtowrite));
//$stringtowrite=utf8_decode($stringtowrite);
echo $stringtowrite;
$myFile = "yyy.txt";
$fh = fopen($myFile, 'w+b') or die("can't open file");
fwrite($fh, $stringtowrite);
fclose($fh);
echo "<a href='".$myFile."'>Download</a>";

i`ve tried ... just about everything. nothing works. how do you use iconv ?
 
Code:
$text = "some weird text";
$_text = iconv('input charset' ,'output charset//TRANSLIT', $text);

 
So ... after 2 days of trying and a lot of hair lost ... found the solution. I`ll post it just in case someone might run into the same problem

First of all, i wasn`t getting the characters the right way. Had to set the charset in the page`s header to UTF-8

Second, although i had the database varchar fields set to utf8_general_ci, had to set the connection type right :

mysql_query("SET CHARACTER SET utf8", $con);
mysql_query("SET NAMES 'utf8'", $con);

[where $con is the connection to the database, with server/user/pwd]

Third, when inserting into the database, had to convert the $_POST string to utf8, using the mysql convert function :

$sql="INSERT INTO foo (id, text) VALUES ('".$id."',convert('".$string."' using utf8))";

That took care of the inserting correctly into the database [and not getting the &amp;#12345 in there]

For the file writing ... that was pretty easy. I just had to set the correct file type header to UTF-8 [EF BB BF], and just writing the string you get, without any conversions whatsoever:

$myFile="a.txt";
$fh = fopen($myFile, 'wb') or die("can't open file");
fwrite($fh,pack("CCC",0xef,0xbb,0xbf));
fwrite($fh, $string);
fclose($fh);

Anyway, hope this will help someone in the future. Over and out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top