显示写入文件的 UTF 时缺少字符


Missing characters in displaying UTF that written 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);
    }
?>

那么如何将UTF8(或任何多语言)写入文件的正确方法呢?

如果您确保所有页面都已经在使用 UTF-8,那么解决方案将是: 对文件不做任何特殊操作,只需将字符串(已经是 UTF-8)写入文件即可。

所以现在你需要了解如何在你的页面中的所有内容都是UTF-8。您应该开始发送Content-type标头:header('Content-type: text/html; charset=utf-8');