Reputation: 1447
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.
Upvotes: 0
Views: 2583
Reputation: 197564
If you need the string in UTF-8 regardless of the php-script-file-encoding (if it's a single-byte one), you should use the UTF-8 encoding of those characters:
$output_string = "string with characters like \xC3\xA3 or \xC3\x8C";
Upvotes: 0
Reputation: 2291
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.
Upvotes: 0
Reputation: 2206
Try to add a BOM (Byte Order Mark) to your file :
$output_string = "\xEF\xBB\xBF";
$output_string .= "string with characters like ã or ì";
$fileHandle = // ...
Upvotes: 4
Reputation: 522005
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.
Upvotes: 1