doudu2404 2012-12-18 12:00
浏览 52
已采纳

fwrite中的UTF-8字符

I'm trying to save HTML to a .html file,

This is working:

$html_file = "output.html";
$output_string="string with characters like ã or ì";
$fileHandle = fopen($html_file, 'w') or die("file could not be accessed/created");
fwrite($fileHandle, $output_string);
fclose($fileHandle);

When I check the output.html file, these special characters in my output_string are not read correctly.

My HTML file can't have a <head> tag with the charset information, this makes it work, but my output can't have any <html>, <head> or <body> tags.

I have tried stuff like

header('Content-type: text/plain; charset=utf-8');

I also tried utf8_encode() on the string before fwrite, but with no success so far.

If I read the output.html file in Notepad++ or Netbeans IDE, it shows the correct characters being saved, it's the browser that isn't them reading properly.

I'm pretty sure PHP is saving my file with the incorrect charset, because if I create HTML files in my computer with those special characters (even without any charset setting), these are read correctly.

  • 写回答

4条回答 默认 最新

  • doupi6737 2012-12-18 12:04
    关注

    Try to add a BOM (Byte Order Mark) to your file :

    $output_string = "\xEF\xBB\xBF";
    $output_string .= "string with characters like ã or ì";
    $fileHandle = // ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题