fwrite() and UTF8

后端 未结 8 835
隐瞒了意图╮
隐瞒了意图╮ 2020-12-05 16:02

I am creating a file using php fwrite() and I know all my data is in UTF8 ( I have done extensive testing on this - when saving data to db and outputting on normal webpage a

相关标签:
8条回答
  • 2020-12-05 16:50

    If you know the data is in UTF8 than you want to set up the header.

    I wrote a solution answering to another tread.

    The solution is the following: As the UTF-8 byte-order mark is \xef\xbb\xbf we should add it to the document's header.

    <?php
    function writeStringToFile($file, $string){
        $f=fopen($file, "wb");
        $file="\xEF\xBB\xBF".$file; // this is what makes the magic
        fputs($f, $string);
        fclose($f);
    }
    ?>
    

    You can adapt it to your code, basically you just want to make sure that you write a UTF8 file (as you said you know your content is UTF8 encoded).

    0 讨论(0)
  • 2020-12-05 16:54

    I know all my data is in UTF8 - wrong.
    Encoding it's not the format of a file. So, check charset in headers of the page, where you taking data from:
    header("Content-type: text/html; charset=utf-8;");
    And check if data really in multi-byte encoding:
    if (strlen($data)==mb_strlen($data, 'UTF-8')) print 'not UTF-8';
    else print 'utf-8';

    0 讨论(0)
提交回复
热议问题