Search code examples
phpunicodeencodingdecoding

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


Solution

  • 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.