简体   繁体   中英

Decode unicode escape characters with PHP

I have multiple text files to translate an application into different languages. These text files come from a Java application and look like this:

weather_501=m\u00E4ßiger Regen
weather_701=tr\u00FCb
weather_731=Sand / Staubsturm
weather_802=\u00FCberwiegend bew\u00F6lkt

I use the following PHP function to load this file, $locale is the language for the output:

function loadTranslation($locale) {
    $this->_data = array();
    $localeFilePath = PATH_LANG . '/text/textTx_' . $locale . '/properties';

    if (file_exists($localeFilePath)) {
        $localeFileHandle = fopen($localeFilePath, "r");        
        while (($line = fgetcsv($localeFileHandle, 1000, "=")) !== FALSE) {
            $this->_data[$line[0]] = $line[1];
            print_r($line);
        }
        fclose($localeFileHandle);
    }
    else echo 'Error: localeFilePath does not exist: '.$localeFilePath.'<br/>';

    return $this;
}

The problem now is the unicode decoding/encoding to UTF-8. I have tried several attempts, but nothing was really working. For example, Decode unicode escape characters which was working for the German Umlauts, but not for chars like 'ß'. Output "mä iger Regen". Furtermore I didn't get the json_encode or json_decode functions to work...

Maybe someone could help me out here and tell me what I'm doing wrong.

Note: PHP version 5.5

The problem was the text files for the simple reason that they were encoded wrongly.

The files are now encoded properly and everything is working fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM