I developing a web using PHP, MySQL, Ajaax, JavaScript and jQuery, and I have problems with special characters.
I have saved data on a DDBB using MySQL, and I don't know how I have to save the used special characters (like á, ñ, '...).
I have tried with HTML number, HTML name and Hex, but when I display it on my website they appear like á
á
or whatever I try.
Some advice?
Thanks
EDIT: I finally solved this using this PHP code por encode:
<?php
function cleanString($text) {
global $charesult;
$utf8 = array(
"/'/" => "'",
'/%/' => '%',
'/:/' => ':',
'/</' => '<',
'/=/' => '=',
'/>/' => '>',
'/–/' => '-',
'/@/' => '@',
'/_/' => '_',
'/`/' => '`',
'/{/' => '{',
'/}/' => '}',
'/~/' => '~',
'/‘/' => '‘',
'/’/' => '’',
'/‚/' => '‚',
'/“/' => '“',
'/”/' => '”',
'/„/' => '„',
'/À/' => 'À',
'/Á/' => 'Á',
'/Â/' => 'Â',
'/Ã/' => 'Ã',
'/Ä/' => 'Ä',
'/Å/' => 'Å',
'/Ç/' => 'Ç',
'/È/' => 'È',
'/É/' => 'É',
'/Ê/' => 'Ê',
'/Ë/' => 'Ë',
'/Ì/' => 'Ì',
'/Í/' => 'Í',
'/Î/' => 'Î',
'/Ï/' => 'Ï',
'/Ñ/' => 'Ñ',
'/Ò/' => 'Ò',
'/Ó/' => 'Ó',
'/Ô/' => 'Ô',
'/Õ/' => 'Õ',
'/Ö/' => 'Ö',
'/Ù/' => 'Ù',
'/Ú/' => 'Ú',
'/Û/' => 'Û',
'/Ü/' => 'Ü',
'/à/' => 'à',
'/á/' => 'á',
'/â/' => 'â',
'/ã/' => 'ã',
'/ä/' => 'ä',
'/ç/' => 'ç',
'/è/' => 'è',
'/é/' => 'é',
'/ê/' => 'ê',
'/ë/' => 'ë',
'/ì/' => 'ì',
'/í/' => 'í',
'/î/' => 'î',
'/ï/' => 'ï',
'/ñ/' => 'ñ',
'/ò/' => 'ò',
'/ó/' => 'ó',
'/ô/' => 'ô',
'/õ/' => 'õ',
'/ö/' => 'ö',
'/ù/' => 'ù',
'/ú/' => 'ú',
'/û/' => 'û',
'/ü/' => 'ü',
);
$charesult = preg_replace(array_keys($utf8), array_values($utf8), $text);
}
?>
And this JS code for decode:
function chardecode(texto){
txt = ""
for (i = 0; i < texto.length - 1; i++){
if ((texto[i] + texto[i+1]) == "&#"){
t = "";
for (j = i + 2; j < texto.indexOf(";",i); j++){
t = t + texto[j];
}
txt = txt + String.fromCharCode(t);
i = j;
} else {
txt = txt + texto[i];
}
}
if (texto[texto.length - 1] != ";"){
txt = txt + texto[texto.length - 1];
}
return txt;
}