Stefan
Stefan

Reputation: 2204

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.

Upvotes: 6

Views: 2348

Answers (2)

Niko
Niko

Reputation: 26730

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;
}

Upvotes: 5

Tei
Tei

Reputation: 1416

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" />

Upvotes: 1

Related Questions