Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference Firefox - Chrome when encoding umlauts

Chrome converts this: aöüß to %C3%A4%C3%B6%C3%BC%C3%9F But Firefox converts it to this strange thing here: a%F6%FC%DF I can't seem to find a way to convert the Firefox thing back to the original in PHP. Urldecode and rawurldecode unfortunately don't work. Does anyone know how to deal with that? Thanks.

like image 461
Stefan Avatar asked Apr 02 '12 10:04

Stefan


2 Answers

As Tei already guessed: Chrome is using UTF-8 (as probably recommended) for URL parameters while Firefox uses Latin-1. I don't think that you can control this behavior. Also this will be difficult to handle, because you pretty much need to guess the encoding that was used.

This is how the decoding works (browser-dependent, assuming that you're using UTF-8 in your application):

Chrome:

$text = urldecode($_GET['text']);

Firefox:

$text = utf8_encode(urldecode($_GET['text']));

This may be a solution that works in most cases:

function urldecode_utf8($text) {
    $decoded = urldecode($text);

    if (!mb_check_encoding($decoded, 'UTF-8')) {
        $decoded = utf8_encode($decoded);
    }

    return $decoded;
}
like image 89
Niko Avatar answered Oct 22 '22 18:10

Niko


Force your page to use UTF-8. Probably these codes are different encoded umlauts. One is something like Latin1, and the other perhaps is UTF-8.

The best way to force utf-8 is in a meta tag in the html.

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
like image 27
Tei Avatar answered Oct 22 '22 17:10

Tei