UTF-8 encoded HTML pages show � (questions marks) instead of characters
When you save a file as UTF-8, configure your server headers correctly, and yet see replacement characters like � instead of expected accented letters, the issue almost never lies in the HTTP headers. It usually points to a mismatch between how the text editor saved the file and what the script actually outputs.
The source material describes a scenario where the developer explicitly set their environment to UTF-8: the HTML file was intended to be saved without a BOM, the .htaccess file included AddCharset UTF-8 .php, and the PHP code outputted a header of Content-Type: text/html; charset=UTF-8. Despite these settings, browsers detected the content as windows-1252 or ISO-8859-1. The crucial clue in the original problem statement was an update admitting that the file was actually saved with ISO_8859-1 encoding due to incorrect project settings.
This is a classic case of the "bom confusion" or simply saving in the wrong character set. Modern browsers are notoriously forgiving when it comes to encoding detection. They will often ignore explicit meta tags or HTTP headers if the byte sequence at the beginning of the file suggests a different encoding. If your text editor saves a file as ISO-8859-1, the bytes representing an é are different from those in UTF-8. When the server sends these bytes with a header claiming they are UTF-8, the browser tries to decode the ISO-8859-1 bytes assuming they are UTF-8, resulting in garbled text or replacement characters.
The fix is straightforward but requires discipline: ensure your source code files match the declared encoding. If you configure your project settings in your IDE to "UTF-8", verify that this applies to all new files and existing files. In many environments, including older versions of XAMPP on Windows, default template files might inherit legacy encodings like ISO-8859-1. You can often check this by opening the file in a hex editor or using a command-line tool like file (on Linux/Mac) or specific extensions in your code editor that show encoding status.
Beyond the initial file saving issue, there are other layers of configuration that must align to prevent these rendering issues. The meta tag <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> is a strong hint, but it is not a guarantee. If the byte stream does not start with the UTF-8 BOM (for older applications) or if the bytes themselves are not valid UTF-8, the browser will override the meta tag based on what it detects from the first few bytes of the file.
When using PHP, relying solely on header('Content-Type:text/html; charset=UTF-8'); is good practice, but it cannot fix a source file that was saved in a different encoding. The AddCharset UTF-8 .php directive in .htaccess is less effective than explicitly setting the header in the PHP script itself, as Apache's AddCharset directive can sometimes be overridden or behave inconsistently depending on the server configuration.
To ensure consistency across your application, consider moving away from relying on browser sniffing or default encodings. Always explicitly set the charset at the very top of every response. Additionally, if you are working with databases and legacy systems, ensure that your database connection also uses UTF-8. A common pitfall occurs when the PHP script outputs UTF-8 text but reads it from a MySQL database configured for latin1. The database returns bytes that look correct in latin1 but are interpreted incorrectly as UTF-8 by the browser, leading to the same replacement character symptoms.
In modern development stacks, particularly when using frameworks like Laravel, the default configuration often handles this well out of the box. However, if you are maintaining legacy code on Windows servers, you may need to audit your template files and editor settings manually. The transition from ISO-8859-1 to UTF-8 is not just about changing a setting; it requires ensuring that every single file in the project tree is saved correctly before deployment.
If you encounter this issue after switching encodings, check for hidden BOMs. Some editors add a Byte Order Mark at the start of a file when saving as UTF-8. While modern browsers handle UTF-8 without a BOM perfectly, some older parsers or specific server configurations might mishandle files with a BOM combined with explicit charset headers. If you see issues persisting after fixing the encoding, try saving the file again without the BOM option to rule out this edge case.
Ultimately, the path to correct rendering is strict adherence to one encoding throughout the entire stack: editor, database, and HTTP response. Once the bytes in the file match the header declaration, the � characters will disappear, and your special characters like é, á, and ¡ will render correctly regardless of what the browser initially detects.
