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).

answered Aug 31 '12 at 12:04

 
   
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

fwrite() and UTF8 转载的更多相关文章

  1. 字符编码笔记:ASCII,Unicode和UTF-8(转载)

    作者: 阮一峰 日期: 2007年10月28日 今天中午,我突然想搞清楚Unicode和UTF-8之间的关系,于是就开始在网上查资料. 结果,这个问题比我想象的复杂,从午饭后一直看到晚上9点,才算初步 ...

  2. mysql 5.5 编码设置为utf8 转载自:http://outofcontrol.ca/thoughts/comments/change-mysql-5.5-default-character-set-to-utf8

    Change MySQL 5.5 default character-set to UTF8 连接里是linux下的 在window下my.ini Add under [client]  the fo ...

  3. CMD命令之 :修改windows的CMD窗口输出编码格式为UTF-8

    修改windows的CMD窗口输出编码格式为UTF-8 转载自 http://xuliduo.iteye.com/blog/639923 dos命令: chcp 65001  就是换成UTF-8代码页 ...

  4. ajax乱码问题 服务端 客户端 两种的解决方案--转载

    今天弄了一天的Ajax中文乱码问题,Ajax的乱码问题分为两种: 1. JavaScript输出的中文乱码, 比如:alert("中文乱码测试"); 2. 这第二种就是Ajax从服 ...

  5. json.decoder.JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig): line 1 column 1

    问题描述:使用Python代码将txt城市列表文件转换为xls文件,源码如下, #!/usr/bin/env Python # coding=utf-8 import os import json i ...

  6. Sublime Text通过插件编译Sass为CSS及中文编译异常解决

    虽然PostCSS才是未来,但是Sass成熟稳定,拥有一大波忠实的使用者,及开源项目,且最近Bootstrap 4 alpha也从Less转到Sass了.所以了解Sass还是非常有必要的. 基于快速开 ...

  7. PHP+JQUEY+AJAX实现分页【转】

    HTML CSS #list{width:680px; height:530px; margin:2px auto; position:relative} #list ul li{float:left ...

  8. Navicat Premium下sql导入中文乱码解决方案

    今天帮忙朋友找bug的时候,准备导入她数据库里面的数据,所以我就试图在mysql管理工具Navicat下面导入相应的mysql命令.结果发现导入的中文字符全部变成乱码,所以做了如下这种尝试: 在“连接 ...

  9. 《Pandoc用户指南》之一

    @(编程) 1. 描述 Pandoc是一个用于从一种标记格式转换为另一种的Haskell库,还是一个使用该库的命令行工具.它可以读取markdown格式和Textile格式(的子集).reStruct ...

随机推荐

  1. Orderly Class

    题目链接: https://nanti.jisuanke.com/t/40449 题目大意:给出两个长度相同的不同字符串A, B.可以对A的任意长度区间进行一次翻转,问有多少种方法可以使得翻转后两字符 ...

  2. SQL SERVER CONVERT函数

    定义: CONVERT函数返回 转换了数据类型的数据. 语法: CONVERT(target_type,expression,date_style smallint) 参数: ①target_type ...

  3. oracle父子级查询数据树结构

    select t.*, level , sys_connect_by_path (t .id, '-->') as tree from isc_res_res_r t connect by pr ...

  4. CodeBlocks 配置

    CodeBlocks 配置 Code::Blocks 17.12 时间:2019.6 下载网址 http://www.codeblocks.org/downloads/26 ,这里选择的是 mingw ...

  5. Comet OJ Contest #15 D. 双十一特惠(困难版)

    以 $d(x)$ 表示正整数 $x$ 的十进制表示的数位之和.熟知下列关于 $d(x)$ 的结论: $d(x) \equiv x \pmod{9}$.从而对于任意正整数列 $a_1, a_2, \do ...

  6. Docker 镜像的制作

        1.登录docker            docker ecex –it 容器名/容器id /bin/bash        例如:            docker exec –it t ...

  7. webpack打包时删除console.log,和debugger

    开发过程中我们不可避免的需要console.log调试,然而在上线时如果不删除这些console.log可能会造成内存泄漏,因为console.log出来的变量是不会被GC的,webpack给我们提供 ...

  8. javascript之取余数、去商、天花板取整、地板取整

    demo1: console.log('//求余数'); //求余数 console.log(5 % 4); console.log(6 % 4); //求商 console.log('//求商'); ...

  9. spring boot logback无感配置

    spring boot1.5.x版本的日志配置一直有一个问题,就是不能直接通过yml配置文件进行日志文件大小进行动态和方便的配置. 怎么解决?直接在springboot项目的maven工程中的src/ ...

  10. js 怎样判断用户是否在浏览当前页面

    有些时候我们需要在项目中判断用户是否在浏览当前页面,或者当前页面是否处于激活状态.然后再进行相关的操作.浏览器中可通过window对象的onblur.onfocus判断,或者document的hidd ...