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.
Asked
Active
Viewed 2,278 times
6

Stefan
- 2,164
- 1
- 23
- 40
2 Answers
5
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;
}

Niko
- 26,516
- 9
- 93
- 110
-
Thank you, that worked. But these different Browser behaviors just suck -.- – Stefan Apr 02 '12 at 11:32
-
Yeah, I'm actually surprised that Firefox still sticks to Latin-1 here. – Niko Apr 02 '12 at 11:48
-
@Niko Firefox actually uses an encoding that depends on the user's locale by default. Turns out that doing what Chrome does breaks a bunch of websites outside the US.... – Boris Zbarsky Apr 02 '12 at 16:16
1
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" />

Tei
- 1,400
- 8
- 6
-
I already tried to force UTF-8 by sending a Header with PHP and using html meta tags, nothing worked. And it's not a form btw, just a normal get parameter ?text=aöüß – Stefan Apr 02 '12 at 11:11
-
?text=aöüß is not urlencoded. To encode in javascript you need encodeURIcomponent, to preserve the encoding. To encode in PHP, you can use rawurlencode. I don't know if you will need or not rawurldecode if you rawurldecode a item.. – Tei Apr 02 '12 at 11:22
-
Append ?text=aöüß to a page in Firefox and you'll see that it automatically encodes it to a%F6%FC%DF – Stefan Apr 02 '12 at 11:26
-
It doesn't do that here (stackoverflow) ... and this page is utf-8. Tested with FF11 on Win7Pro – devnull69 Apr 02 '12 at 13:22