1000 FAQs, 500 tutorials and explanatory videos. Here, there are only solutions!
Solve page encoding issues
This guide is for you if you encounter encoding problems with your pages and strange characters appearing on them.
Universal Encoding
If you are experiencing page encoding issues, it's important to ensure that your page encoding is set to UTF-8. UTF-8 is a universal encoding that supports most characters used worldwide. It's possible that some files contain characters incompatible with UTF-8 encoding. In this case, you need to convert them to UTF-8 using an appropriate text editor.
If the HTML page encoding is set to UTF-8 while the database is in latin-1, accents may not be interpreted correctly by MySQL. If pages contain special characters (Arabic, 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 are importing text files with SQL code inside, you need to edit them with software that saves in UTF-8.
UTF-8 in PHP
To force UTF-8 headers on your site using PHP, you can use the header()
function with the Content-Type
parameter. Here's an example of code to force 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>
Also, ensure that all files used on your website, such as CSS 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 Database
To (re)declare database and table encoding 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, look for 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 "Go" 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 don't specify it, the default value of this parameter may vary from server to server):$connection = mysql_connect($server, $username, $password);
mysql_set_charset('utf8', $connection);
You can also change this default setting for your entire 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 (e.g., "?").
MySQL database backups are made in UTF8 format. Accented characters are then coded on two bytes. Importing these backups poses no problem on the servers, however, you need to adjust your local configuration for importing UTF8 databases on your computer.
For example, you can convert utf8 -> latin1 from the SQL file before importation.
Otherwise, when importing the database, 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 non-UTF8 format and the software is configured to import a UTF-8 file. Linux can convert to UTF-8 with the 'iconv' command.