简体   繁体   中英

UTF-8 characters in fwrite

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.

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

$output_string = "\xEF\xBB\xBF";
$output_string .= "string with characters like ã or ì";
$fileHandle = // ...

Yes, PHP is writing the file correctly, only the reading program doesn't know what character encoding it is and interprets the data with the wrong charset. If you cannot include meta information that convey the correct charset and if the file format itself (plain text) does not offer a way to specify the charset and if the reading application is not able to correctly guess the charset, then there's no solution.

Whatever editor you are using to write this code must have facility to set character-type as 'UTF-8'.

Set the character-type of the file in which you have written this code. I am using an editor that allows to change the character encoding of file from the bottom. There must be something similar for the editor you are using.

如果不管php-script-file-encoding是什么(如果是单字节形式),都需要UTF-8中的字符串,则应使用这些字符的UTF-8编码:

$output_string = "string with characters like \xC3\xA3 or \xC3\x8C";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM