G
In a quick answer: No It's possible.At the time the page starts rendering, it already assumes a coding (or tries to detect it happens not to be declared, usually uses the server pattern).Common coding problemsIt is very common when we are working with accents to come across strange characters such as:Something similar to é (which represents the é) or ã (which represents ã), this occurs because the character is unicode, but the page is in iso-8859-1 (or other compatible).And the signal � a situation example is when you use an iso-8859-1-compatible accents on a page you are trying to process UTF-8 due to Content-Type: ...; charset=utf8.About iso-8859-1I recommend using iso-8859-1 if your site is "100% in Portuguese" and you do not need "extra codes" (e.g. emojis), however even Portuguese you should think about maybe migrating pro utf-8, one of the reasons is that in June 2004, the ISO/IEC development group responsible for its maintenance declared the end of support for this coding, focusing on UCS and Unicode.Next I will explain how to use each of them, according to the topics:What is required to use UTF-8What is required to use iso-8859-1/latin1/ansiWhat is required to use UTF-8PHP scripts (I refer to the files on the server and not the answer of it) saved in "utf-8 without BOM"MySQL (or other type of database) with charset=utf-8Preferably define using PHP header('Content-type: text/html; charset=UTF-8');Note: The advantage of UTF-8 is that you can use multiple "languages" on your page with characters that are not supported by "iso-8859-1".Source: http://pt.wikipedia.org/wiki/ISO_8859-1 If you decide to use UTF-8 on your site/project, I recommend following the following steps:PHP Scripts with UTF-8 without "BOM"Note: read about this in http://en.wikipedia.org/wiki/UTF-8#Byte_order_mark (English)You must save all PHP scripts (even those you will use with include, require, etc) in utf-8 without "BOM", you can use software like http://www.sublimetext.com/ or http://notepad-plus-plus.org/ to convert the files:Using notepad++:Using Sublime Text:Using Eclipse go to Window > Preferences > General > Workspace > Text File Encoding:Note: that files with extension .js or .css that use accents in the content also be saved with the same encoding as the pages, in the same way it was used to save the documents described above, eventually files .svg can then be shipped if they have any different accent or character should also be saved with the same page encodingMySQL with UTF-8To create a UTFf-8 table in MySQL you should use something like:CREATE TABLE minhatabela (
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
titulo varchar(300) DEFAULT NULL
) ENGINE=InnoDB CHARACTER SET=utf8 COLLATE utf8_unicode_ci;
If the tables exist, first make a BACKUP of them and then use one of the following commands (as required):Convert database:
ALTER DATABASE bancodedados CHARACTER SET utf8 COLLATE utf8_unicode_ci;Convert a specified table:
ALTER TABLE minhatabela CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;In addition to creating the tables in UTF-8 it is necessary to define the connection as UTF-8.With PDO it is necessary to use http://php.net/manual/pt_BR/pdo.exec.php :$conn = new PDO('mysql:host=HOST;dbname=BANCO;charset=utf-8', 'USUARIO', 'SENHA');
$conn->exec('SET CHARACTER SET utf8');//Define o charset como UTF-8
With mysqli it is necessary to use http://php.net/manual/pt_BR/mysqli.set-charset.php :$mysqli = new mysqli('HOST', 'usuario', 'senha', 'banco');
if ($mysqli->connect_error) {
printf('Erro de conexão: %s', $mysqli->connect_errno);
exit;
}
/*
compatibilidade para to 5.2.9 e 5.3.0.
*/
if (mysqli_connect_error()) {
printf('Erro de conexão: %s', mysqli_connect_error());
exit;
}
if (false === $mysqli->set_charset('utf8')) {
printf('Error ao usar utf8: %s', $mysqli->error);
exit;
}
With mysqli procedural:<?php
$link = mysqli_connect('HOST', 'usuario', 'senha', 'banco');
if (mysqli_connect_error()) {
printf('Erro de conexão: %s', mysqli_connect_error());
exit;
}
if (!mysqli_set_charset($link, 'utf8')) {
printf('Error ao usar utf8: %s', mysqli_error($link));
exit;
}
Setting the page charsetYou can use the tag <meta> to set the charset, but the recommended is that you do this in the request response (server response), setting the "headers" (this does not mean that you should not use <meta>).For this, in PHP use the function http://php.net/manual/pt_BR/function.header.php :The reason to use in the server response is also because of the page rendering time as to the server response and pages AJAX need also the charset set by header();.Note: header(); should always go at the top of the script before any echo, print, or other type of content display.In files that the answer should be HTML:<?php
header('Content-Type: text/html; charset=UTF-8');
echo 'Conteudo';
What is required to use iso-8859-1/latin1/ansiTo use iso-8859-1, you should use:PHP scripts saved in "iso-8859-1" (or windows-1252 and ansi)MySQL (or other type of database) with charset=iso-8859-1Preferably define using PHP header('Content-type: text/html; charset=iso-8859-1');First you must save "all .php scripts" as iso-8859-1 (or ANSI) and .html documents (if any):To save using notepad++: To save using SublimeText: MySQL with Latin1Almost all mysql servers have been configured to use by default the charset latin1, however this may be modified my.ini, then the following steps on mysql are optional as they will depend on your my.ini being using something different the latin1 in the following lines:[client]
default-character-set=<charset padrão para o cliente>
[mysql]
default-character-set=<charset padrão para o cliente>
Then you need to define latin1, with PDO it is necessary to use http://php.net/manual/pt_BR/pdo.exec.php :$conn = new PDO('mysql:host=HOST;dbname=BANCO;charset=utf-8', 'USUARIO', 'SENHA');
$conn->exec('SET CHARACTER SET latin1');//Define o charset como UTF-8
And with mysqli it is necessary to use http://php.net/manual/pt_BR/mysqli.set-charset.php :$mysqli = new mysqli('HOST', 'usuario', 'senha', 'banco');
if ($mysqli->connect_error) {
printf('Erro de conexão: %s', $mysqli->connect_errno);
exit;
}
/*
compatibilidade para to 5.2.9 e 5.3.0.
*/
if (mysqli_connect_error()) {
printf('Erro de conexão: %s', mysqli_connect_error());
exit;
}
if (false === $mysqli->set_charset('latin1')) {
printf('Error ao usar latin1: %s', $mysqli->error);
exit;
}
With mysqli procedural:<?php
$link = mysqli_connect('HOST', 'usuario', 'senha', 'banco');
if (mysqli_connect_error()) {
printf('Erro de conexão: %s', mysqli_connect_error());
exit;
}
if (!mysqli_set_charset($link, 'latin1')) {
printf('Error ao usar latin1: %s', mysqli_error($link));
exit;
}
Setting the page charsetJust add to ; charset=iso=8859-1 after type, for example text/html or application/xml or text/plain:<?php
header('Content-Type: text/html; charset=iso-8859-1');
echo 'Conteudo';