search

Home  >  Q&A  >  body text

How to convert 'u00e9' to utf8 characters in mysql or php?

<p>I am doing data cleaning on some messy data that I am importing into mysql. </p> <p>The data contains "pseudo" unicode characters that are actually embedded in the string, such as "u00e9" etc. </p> <p>So a field might be.."Jalostotitlu00e1n" I need to rip off that awkward 'u00e1n' and replace it with the corresponding utf character</p> <p>I could do this in mysql, maybe using substrings and CHR, but I'm preprocessing the data via PHP, so I can do it there as well. </p> <p>I already know how to configure mysql and php to use utf data. The problem actually lies in the source data I imported. </p> <p>Thank you</p>
P粉704196697P粉704196697467 days ago709

reply all(2)I'll reply

  • P粉743288436

    P粉7432884362023-08-25 13:53:33

    /* php function to convert utf8 html to ansi */

    public static function Utf8_ansi($valor='') {
    
        $utf8_ansi2 = array(
        "\u00c0" =>"À",
        "\u00c1" =>"Á",
        "\u00c2" =>"Â",
        "\u00c3" =>"Ã",
        "\u00c4" =>"Ä",
        "\u00c5" =>"Å",
        "\u00c6" =>"Æ",
        "\u00c7" =>"Ç",
        "\u00c8" =>"È",
        "\u00c9" =>"É",
        "\u00ca" =>"Ê",
        "\u00cb" =>"Ë",
        "\u00cc" =>"Ì",
        "\u00cd" =>"Í",
        "\u00ce" =>"Î",
        "\u00cf" =>"Ï",
        "\u00d1" =>"Ñ",
        "\u00d2" =>"Ò",
        "\u00d3" =>"Ó",
        "\u00d4" =>"Ô",
        "\u00d5" =>"Õ",
        "\u00d6" =>"Ö",
        "\u00d8" =>"Ø",
        "\u00d9" =>"Ù",
        "\u00da" =>"Ú",
        "\u00db" =>"Û",
        "\u00dc" =>"Ü",
        "\u00dd" =>"Ý",
        "\u00df" =>"ß",
        "\u00e0" =>"à",
        "\u00e1" =>"á",
        "\u00e2" =>"â",
        "\u00e3" =>"ã",
        "\u00e4" =>"ä",
        "\u00e5" =>"å",
        "\u00e6" =>"æ",
        "\u00e7" =>"ç",
        "\u00e8" =>"è",
        "\u00e9" =>"é",
        "\u00ea" =>"ê",
        "\u00eb" =>"ë",
        "\u00ec" =>"ì",
        "\u00ed" =>"í",
        "\u00ee" =>"î",
        "\u00ef" =>"ï",
        "\u00f0" =>"ð",
        "\u00f1" =>"ñ",
        "\u00f2" =>"ò",
        "\u00f3" =>"ó",
        "\u00f4" =>"ô",
        "\u00f5" =>"õ",
        "\u00f6" =>"ö",
        "\u00f8" =>"ø",
        "\u00f9" =>"ù",
        "\u00fa" =>"ú",
        "\u00fb" =>"û",
        "\u00fc" =>"ü",
        "\u00fd" =>"ý",
        "\u00ff" =>"ÿ");
    
        return strtr($valor, $utf8_ansi2);      
    
    }

    reply
    0
  • P粉198814372

    P粉1988143722023-08-25 00:30:22

    There is a way. Replace all uXXXX with their HTML representation and execute html_entity_decode()

    That isecho html_entity_decode("Jalostotitlán");

    Every UTF character of the form

    u1234 can be printed in HTML as . But doing the replacement is very difficult because if there are no other characters to identify the beginning of the UTF sequence, you can get a lot of false positives. A simple regular expression might be

    preg_replace('/u([\da-fA-F]{4})/', '&#x\1;', $str)

    reply
    0
  • Cancelreply