UTF-8 and websites

Is your website in other language than english? Does your website contain characters that are not in the english alphabet? Then you really need your website to accept UTF-8 encoding. Of course, there are more standards (ISO 8859-1, ISO 8859-2 etc), but UTF-8 gets all the characters you need.

  1. UTF-8 and HTML – In order to add UTF-8 characters in your static HTML files, open the files using an file editor (Notepad++, Notepad2) and change encoding to UTF-8;
    • Notepad++ : Top menu -> Encoding -> Encode in UTF-8
    • Notepad2 : Top Menu -> File -> Encoding-> UTF-8

    For the browsers to know what encoding type you are using, add this line in the <head> section of your website:

    <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>

  2. UTF-8 and Javascript – If you need to write some sentences that will be replaced using javascript and are stored in .js files, you have to act as with HTML files;
  3. UTF-8 and PHP – In order to work with UTF-8 strings in PHP, act exactly as with HTML files. Add this line in your php files

    <?php header(“Content-type: text/html; charset=utf-8″);?>

    Be sure to not output any character before inserting this line.  Beware using functions that you must explicitly say that you want to work with UTF-8 strings, such as htmlentities and htmlspecialchars.

    • Only the files that contain UTF-8 strings must have UTF-8 encoding.  The other .php files can be eighter UTF-8 or other Encoding format
  4. UTF-8 and MySQL – If you want to store UTF-8 strings in your MySQL database, you have to make some changes to the fields you will store those strings into.  For CHAR/VARCHAR/TEXT  fields (and all derived by these), by default mysql sets Collation to  latin_swedish_ci . This encoding does not store UTF-8 characters.  So you have to change the Collation into utf_general_ci.With this done, you now have to tell PHP to communicate with mysql using UTF-8 format. In your PHP file that connects to your database, add these 2 lines right after the mysql_select_db function.

    mysql_query(‘SET CHARACTER SET utf8′);
    mysql_query(‘SET SESSION collation_connection =”utf8_general_ci”‘);

    Your code should look like this:

    $dbconnect = @mysql_connect($mysql['host'], $mysql['username'], $mysql['password'], true) or die(‘Unable to connect to server’);
    @mysql_select_db($mysql['db'], $dbconnect) or die(‘Could not find the database’);
    mysql_query(‘SET CHARACTER SET utf8′);
    mysql_query(‘SET SESSION collation_connection =”utf8_general_ci”‘);

Versiunea in limba Romana / Romanian Version: UTF-8 si siteurile web

Subscribe to RSS Feed Follow me on Twitter!