0

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(
        "/'/"           =>  "&#39;",
        '/%/'           =>  '&#37;',
        '/:/'           =>  '&#58;',
        '/</'           =>  '&#60;',
        '/=/'           =>  '&#61;',
        '/>/'           =>  '&#62;',
        '/–/'           =>  '&#45;',
        '/@/'           =>  '&#64;',
        '/_/'           =>  '&#95;',
        '/`/'           =>  '&#96;',
        '/{/'           =>  '&#123;',
        '/}/'           =>  '&#125;',
        '/~/'           =>  '&#126;',
        '/‘/'           =>  '&#8216;',
        '/’/'           =>  '&#8217;',
        '/‚/'           =>  '&#8218;',
        '/“/'           =>  '&#8220;',
        '/”/'           =>  '&#8221;',
        '/„/'           =>  '&#8222;',
        '/À/'           =>  '&#192;',
        '/Á/'           =>  '&#193;',
        '/Â/'           =>  '&#194;',
        '/Ã/'           =>  '&#195;',
        '/Ä/'           =>  '&#196;',
        '/Å/'           =>  '&#197;',
        '/Ç/'           =>  '&#199;',
        '/È/'           =>  '&#200;',
        '/É/'           =>  '&#201;',
        '/Ê/'           =>  '&#202;',
        '/Ë/'           =>  '&#203;',
        '/Ì/'           =>  '&#204;',
        '/Í/'           =>  '&#205;',
        '/Î/'           =>  '&#206;',
        '/Ï/'           =>  '&#207;',
        '/Ñ/'           =>  '&#209;',
        '/Ò/'           =>  '&#210;',
        '/Ó/'           =>  '&#211;',
        '/Ô/'           =>  '&#212;',
        '/Õ/'           =>  '&#213;',
        '/Ö/'           =>  '&#214;',
        '/Ù/'           =>  '&#217;',
        '/Ú/'           =>  '&#218;',
        '/Û/'           =>  '&#219;',
        '/Ü/'           =>  '&#220;',
        '/à/'           =>  '&#224;',
        '/á/'           =>  '&#225;',
        '/â/'           =>  '&#226;',
        '/ã/'           =>  '&#227;',
        '/ä/'           =>  '&#228;',
        '/ç/'           =>  '&#231;',
        '/è/'           =>  '&#232;',
        '/é/'           =>  '&#233;',
        '/ê/'           =>  '&#234;',
        '/ë/'           =>  '&#235;',
        '/ì/'           =>  '&#236;',
        '/í/'           =>  '&#237;',
        '/î/'           =>  '&#238;',
        '/ï/'           =>  '&#239;',
        '/ñ/'           =>  '&#241;',
        '/ò/'           =>  '&#242;',
        '/ó/'           =>  '&#243;',
        '/ô/'           =>  '&#244;',
        '/õ/'           =>  '&#245;',
        '/ö/'           =>  '&#246;',
        '/ù/'           =>  '&#249;',
        '/ú/'           =>  '&#250;',
        '/û/'           =>  '&#251;',
        '/ü/'           =>  '&#252;',
    );
    $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;
}
AsierUP
  • 27
  • 1
  • 8

0 Answers0