I am trying to input some Vietnamese language from text box to my file then read from that file and display in another page.
The display part is working well as I tried to copy, paste some Vietnamese directly to file and test the displaying. However the writing part some how not right, because when I try input some Vietnamese and test at the display, it will miss some characters at some places. Here is the code I am using to input to file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form name="form" method="post">
<style type="text/css">
.inputtext { width: 550px; height: 550px; }
</style>
<input type="text" name="text_box" class="inputtext" size="250"/>
<input type="submit" id="search-submit" value="SAVE" />
</form>
</body>
</html>
<?php
if(isset($_POST['text_box'])) { //only do file operations when appropriate
$a = $_POST['text_box'];
$myFile = mb_convert_encoding("test.txt", "UTF-8", "auto");
$data = mb_convert_encoding($a, 'UTF-8', "auto");
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh,utf8_encode($data));
fclose($fh);
}
?>
So how is the right way to write UTF8 (or any multi language) to file ?
If you make sure all your pages are already using UTF-8, then the solution would be: Do nothing special with the file, simply write the string (which already is UTF-8) to the file.
So now you need to find out how to make everything in your page UTF-8. You should start sending a Content-type
header: header('Content-type: text/html; charset=utf-8');
in dream weaver u can check include unicode signature (BOM) [CTRL+J]