Handle UTF-8 encoding in PHP, MySQL and HTML

This post describes how to handle the UTF-8 character encoding format in PHP, MySQL and HTML.

Prerequisites

Handle UTF-8 in PHP

The mb_internal_encoding and mb_regex_encoding functions allow to define the encoding used in multi-byte string manipulation.

mb_internal_encoding('UTF-8');
mb_regex_encoding('UTF-8');

The header function allows to send a HTTP header specifying the type and the encoding of the content generated by the PHP script.

For example for a HTML page:

header('Content-Type: text/html; charset=UTF-8');

The source code of PHP scripts must be saved in UTF-8 format.

Handle UTF-8 in MySQL

The mysqli_set_charset function allows to set the character encoding used by the MySQL client.

$Link = mysqli_connect('host', 'login', 'password', 'database');
mysqli_set_charset($Link, 'utf8');

MySQL tables must be created with utf8_general_ci collation.

Handle UTF-8 HTML

The meta tag is used to specify the type and the encoding of the HTML page.

<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />

The source code of HTML pages must be saved in UTF-8 format.

Leave a Reply

Your email address will not be published. Required fields are marked *

*