<i id='Y32bq'><tr id='Y32bq'><dt id='Y32bq'><q id='Y32bq'><span id='Y32bq'><b id='Y32bq'><form id='Y32bq'><ins id='Y32bq'></ins><ul id='Y32bq'></ul><sub id='Y32bq'></sub></form><legend id='Y32bq'></legend><bdo id='Y32bq'><pre id='Y32bq'><center id='Y32bq'></center></pre></bdo></b><th id='Y32bq'></th></span></q></dt></tr></i><div id='Y32bq'><tfoot id='Y32bq'></tfoot><dl id='Y32bq'><fieldset id='Y32bq'></fieldset></dl></div>

    <bdo id='Y32bq'></bdo><ul id='Y32bq'></ul>

<tfoot id='Y32bq'></tfoot>
  • <legend id='Y32bq'><style id='Y32bq'><dir id='Y32bq'><q id='Y32bq'></q></dir></style></legend>

      1. <small id='Y32bq'></small><noframes id='Y32bq'>

      2. 如何以UTF-8格式写入文件?

        时间:2023-10-04

        <small id='whTie'></small><noframes id='whTie'>

              <i id='whTie'><tr id='whTie'><dt id='whTie'><q id='whTie'><span id='whTie'><b id='whTie'><form id='whTie'><ins id='whTie'></ins><ul id='whTie'></ul><sub id='whTie'></sub></form><legend id='whTie'></legend><bdo id='whTie'><pre id='whTie'><center id='whTie'></center></pre></bdo></b><th id='whTie'></th></span></q></dt></tr></i><div id='whTie'><tfoot id='whTie'></tfoot><dl id='whTie'><fieldset id='whTie'></fieldset></dl></div>

              <legend id='whTie'><style id='whTie'><dir id='whTie'><q id='whTie'></q></dir></style></legend>
              • <bdo id='whTie'></bdo><ul id='whTie'></ul>
                    <tbody id='whTie'></tbody>

                  <tfoot id='whTie'></tfoot>
                  本文介绍了如何以UTF-8格式写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一堆不是 UTF-8 编码的文件,我正在将网站转换为 UTF-8 编码.

                  I have bunch of files that are not in UTF-8 encoding and I'm converting a site to UTF-8 encoding.

                  我对要保存为 utf-8 的文件使用了简单的脚本,但这些文件以旧编码保存:

                  I'm using simple script for files that I want to save in utf-8, but the files are saved in old encoding:

                  header('Content-type: text/html; charset=utf-8');
                  mb_internal_encoding('UTF-8');
                  $fpath="folder";
                  $d=dir($fpath);
                  while (False !== ($a = $d->read()))
                   {
                  
                   if ($a != '.' and $a != '..')
                    {
                  
                    $npath=$fpath.'/'.$a;
                  
                    $data=file_get_contents($npath);
                  
                    file_put_contents('tempfolder/'.$a, $data);
                  
                    }
                  
                   }
                  

                  如何以 utf-8 编码保存文件?

                  How can I save files in utf-8 encoding?

                  推荐答案

                  file_get_contents/file_put_contents 不会神奇地转换编码.

                  file_get_contents / file_put_contents will not magically convert encoding.

                  你必须显式地转换字符串;例如 iconv()mb_convert_encoding().

                  You have to convert the string explicitly; for example with iconv() or mb_convert_encoding().

                  试试这个:

                  $data = file_get_contents($npath);
                  $data = mb_convert_encoding($data, 'UTF-8', 'OLD-ENCODING');
                  file_put_contents('tempfolder/'.$a, $data);
                  

                  或者,使用 PHP 的流过滤器:

                  Or alternatively, with PHP's stream filters:

                  $fd = fopen($file, 'r');
                  stream_filter_append($fd, 'convert.iconv.UTF-8/OLD-ENCODING');
                  stream_copy_to_stream($fd, fopen($output, 'w'));
                  

                  这篇关于如何以UTF-8格式写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PHP 输出显示带有问号的黑色小菱形 下一篇:Base64 编码图像

                  相关文章

                  最新文章

                  <small id='s0PTS'></small><noframes id='s0PTS'>

                  <tfoot id='s0PTS'></tfoot>

                  <i id='s0PTS'><tr id='s0PTS'><dt id='s0PTS'><q id='s0PTS'><span id='s0PTS'><b id='s0PTS'><form id='s0PTS'><ins id='s0PTS'></ins><ul id='s0PTS'></ul><sub id='s0PTS'></sub></form><legend id='s0PTS'></legend><bdo id='s0PTS'><pre id='s0PTS'><center id='s0PTS'></center></pre></bdo></b><th id='s0PTS'></th></span></q></dt></tr></i><div id='s0PTS'><tfoot id='s0PTS'></tfoot><dl id='s0PTS'><fieldset id='s0PTS'></fieldset></dl></div>

                      • <bdo id='s0PTS'></bdo><ul id='s0PTS'></ul>
                    1. <legend id='s0PTS'><style id='s0PTS'><dir id='s0PTS'><q id='s0PTS'></q></dir></style></legend>