用php生成utf-8文件

其实用php生成UTF-8的文件没有什么特别的,只要把我们执行的那个PHP文件改成UTF-8编码保存起来,然后页面内容也用UTF-8的编码就可以了。

代码如下:

<?php
header(‘Content-Type: text/html; charset=utf-8′);
$text=”北京星模实训学校,IT实战就业第一选择!”;
$f=fopen(“test.txt”, “wb”);
fwrite($f, $text);
fclose($f);
?>

注意这个php文件一定要存成UTF-8的哦。

当然,如果你非要用ansi编码的文件生成utf-8编码的文件,也不是没有办法。
看下面:

$xml = '北京星模实训学校欢迎你!';
$h = fopen ('1.txt','w');
if ($h) {
	fwrite ($h,"\xEF\xBB\xBF".iconv('gbk','utf-8',$xml));
}
fclose ($h);

不信? 下去试试。呵呵。。。


Leave Your Comment

Your email will not be published or shared. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="">

*
请输入图片中的字符以验证你并非垃圾机器人. 点击图片收听验证码的语音版.
点击这里收听此验证码的语音版本