1000 FAQs, 500 tutorials and explanatory videos. Here, there are only solutions!
Resolve a page encoding issue
This guide is relevant to you if you are experiencing page encoding issues and abnormal characters appearing on your pages.
Universal Encoding
If you encounter page encoding issues, it is important to check that your page encoding is set to UTF-8. UTF-8 is a universal encoding that supports most characters used worldwide. It can happen that some files contain characters that are incompatible with UTF-8 encoding. In this case, you need to convert them to UTF-8 using a suitable text editor.
If the HTML page encoding is, for example, UTF-8 while the database is in latin-1, accents will not be interpreted correctly by MySQL. If the pages contain special characters (Arabic texts, Chinese, accents), they may display correctly on your website but not in phpMyAdmin, or vice versa...
Use a text editor capable of saving your files in UTF-8. If you import text files with SQL code inside, they should be edited with software that saves in UTF-8.
UTF-8 in PHP
To force the site headers to UTF-8 using PHP, you can use the header()
function with the Content-Type
parameter. Here is an example of code that forces character encoding to UTF-8:
<?php
header('Content-Type: text/html; charset=utf-8');
?>
UTF-8 via .htaccess
To force character encoding to UTF-8 via your site's .htaccess file, for HTML content add:
AddDefaultCharset utf-8
Header set Content-Type "text/html; charset=utf-8"
And for PHP content add:
php_value default_charset UTF-8
php_value mbstring.internal_encoding UTF-8
UTF-8 in HTML
To specify character encoding as UTF-8 in HTML code, you can use the charset
meta tag:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Page Title</title>
</head>
<body>
<!-- Page content -->
</body>
</html>
Make sure that all files used on your website, such as CSS style files and JavaScript scripts, are also encoded in UTF-8. This ensures that all characters on your website will be displayed correctly.
UTF-8 in Databases
To (re)declare the encoding of databases and tables as UTF-8 in phpMyAdmin, you can follow these steps:
- Log in to your phpMyAdmin control panel
- Click on the "Operations" tab at the top
- In the "Table Options" section, find the "Collation" line and click on the dropdown button
- Select a collation option that uses UTF-8 encoding, such as
utf8_general_ci
- Click the "Execute" button at the bottom of the page to save the changes
Add the PHP function mysql_set_charset
to the connection created by mysql_connect
to determine the encoding of this connection (if you do not specify it, the default value for this parameter may vary from server to server):
$connection = mysql_connect($server, $username, $password);
mysql_set_charset('utf8', $connection);
You can also modify this default parameter across your hosting by following this documentation.
Once the MySQL connection is established from a PHP script, you can also specify the UTF8 charset type with these commands:
mysql_query("SET NAMES 'utf8';");
mysql_query("SET CHARACTER SET 'utf8';");
Accents / Special Characters
If you retrieve a MySQL dump (backup) and import it into your database, it may happen that all accents are replaced by strange characters (like "?").
MySQL backups are made in UTF8 format. Accented characters are then encoded in two bytes. Importing these backups poses no problem on servers, however, you need to adjust your local configuration for importing UTF8 databases on your machine.
For example, you can perform a conversion from utf8 to latin1 on the SQL file before importing.
Otherwise, during the database import, simply specify that your input file is in iso-latin1 format (import section, "Character set of the file") and the import will proceed correctly.
When special characters are converted to '?', it is likely that the file you are importing is in a non-UTF8 format and the software is configured to import a UTF-8 file. Linux can convert to UTF-8 using the 'iconv' command.