fwrite() and UTF8 转载

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 xefxbbxbf we should add it to the document's header.

<?php
function writeStringToFile($file, $string){
    $f=fopen($file,"wb");
    $file="xEFxBBxBF".$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).

share|improve this answer
 
   
UTF8 BOM meaning 0xEF,0xBB,0xBF, which is precisely what I suggested. Now you obviously can create a file as UTF-8 by changing settings in your IDE. But the same thing can be achieved using just PHP. –  Florin Sima Sep 10 '12 at 15:30
   
+1 This should be the correct answer –  Ergec Aug 14 '13 at 18:38
原文地址:https://www.cnblogs.com/xcwytu/p/3558246.html