Make sure that you are using UTF8 everywhere!

teoria in breve

files i files xml sono coficati in UTF-8, xhtml è codificato in UTF-8 e ogni volta si rende necessario scrivere in una lingua straniera è necessario ricorrere ad una codifica unicode, utf8 è spesso la scelta più ovvia in php.

per motivi storici ci troviamo con la maggior parte dei pregetti scritti con codifiche svariate per convertire un progetto

find . -name "*.php" -exec iconv --verbose -f ISO-8859-1 -t UTF-8 {} -o {}.utf8\;
find . -name "*.php" -exec iconv -f ISO-8859-1 -t UTF-8 {} -o ../newdir_utf8/{} \;

sistemi i server dovrebbero essere settati tutti correttamente per prevenire problemi, verificare come realizzare la cosa altrimenti se non possibile

i files di locale vanno generati per permettere alle funzione della standard c lib di funzionare, php fa delle chiamate al os

sudo locale-gen it_IT.UTF-8

httpd.conf

AddCharset UTF-8 .utf8
AddDefaultCharset UTF-8
 
# altrimenti, se non è disponibile al configurazione, aggiungere direttamente nel codice
 
mb_internal_encoding("UTF-8");

php.ini default_charset = "utf-8"

more details

my.cnf

character-set-server=utf8
default-collation=utf8_unicode_ci
 
appena dopo la connessione a mysql, setta la codifica con
SET NAMES 'utf8';
o (mysqli extension)
mysqli_set_charset('utf8');

make sure your database and tables are all set to the utf8mb4 character set and collation, and that you use the utf8mb4 character set in the PDO connection string.

applicazione SQL

CREATE DATABASE db_name
CHARACTER SET utf8
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci
DEFAULT COLLATE utf8_general_ci;
 
ALTER DATABASE db_name
CHARACTER SET utf8
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci
DEFAULT COLLATE utf8_general_ci ;
 
ALTER TABLE tbl_name DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci ;
 
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");

php

For example, if you use substr() on a UTF-8 string, there’s a good chance the result will include some garbled half-characters.

The correct function to use would be the multibyte counterpart, mb_substr().

The hard part is remembering to use the mb_* functions at all times.

If you forget even just once, your Unicode string has a chance of being garbled during further processing. Not all string functions have an mb_* counterpart. If there isn’t one for what you want to do, then you might be out of luck. Additionally, you should use the mb_internal_encoding() function at the top of every PHP script you write (or at the top of your global include script), and the mb_http_output() function right after it if your script is outputting to a browser. Explicitly defining the encoding of your strings in every script will save you a lot of headaches down the road. Finally, many PHP functions that operate on strings have an optional parameter letting you specify the character encoding. You should always explicitly indicate UTF-8 when given the option. For example, htmlentities() has an option for character encoding, and you should always specify UTF-8 if dealing with such strings.

$number 1212345634.56;
setlocale(LC_ALL'it_IT.utf8');
setlocale(LC_MONETARY'it_IT.utf8');
echo 
money_format('%.2n'$number) . "\n";

header("Content-Type: text/html; charset=utf-8");

mysql_set_charset('utf8');

esempio completo:

<?php
// Tell PHP that we're using UTF-8 strings until the end of the script
mb_internal_encoding('UTF-8');

// Tell PHP that we'll be outputting UTF-8 to the browser
mb_http_output('UTF-8');

// Our UTF-8 test string
$string 'Êl síla erin lû e-govaned vîn.';

// Transform the string in some way with a multibyte function
// Note how we cut the string at a non-Ascii character for demonstration purposes
$string mb_substr($string015);

/*
For an example, see the section on connecting to and querying a MySQL database.
This is critically important.
Note that you must use the utf8mb4 character set for complete UTF-8 support,
not the utf8 character set!
*/
// Note that we define the character set as utf8mb4 in the PDO connection string
$link = new \PDO(   'mysql:host=your-hostname;dbname=your-db;charset=utf8mb4',
                    
'your-username',
                    
'your-password',
                    array(
                        \
PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
                        \
PDO::ATTR_PERSISTENT => false
                    
)
                );

// Store our transformed string as UTF-8 in our database
// Your DB and tables are in the utf8mb4 character set and collation, right?
$handle $link->prepare('insert into ElvishSentences (Id, Body) values (?, ?)');
$handle->bindValue(11PDO::PARAM_INT);
$handle->bindValue(2$string);
$handle->execute();

// Retrieve the string we just stored to prove it was stored correctly
$handle $link->prepare('select * from ElvishSentences where Id = ?');
$handle->bindValue(11PDO::PARAM_INT);
$handle->execute();

// Store the result into an object that we'll output later in our HTML
$result $handle->fetchAll(\PDO::FETCH_OBJ);
?><!doctype html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>UTF-8 test page</title>
    </head>
    <body>
        <?php
        
foreach($result as $row){
            print(
$row->Body);  // This should correctly output our transformed UTF-8 string to the browser
        }
        
?>
    </body>
</html>

assicurarsi di aver installato i pacchetti contenenti i locales di sistema

locale -a
apt-get install language-pack-it language-pack-it-base

html

mb_http_output(); // set browser mode

nella head del documento, inserire il meta tag

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

js il metodo di codifica di default quando si inviano dati in POST è utf-8, ne va tenuto conto in fase di lettura

riferimenti http://mysql.he.net/doc/refman/5.0/en/charset.html man mysql