Tom
Tom

Reputation: 7

UrlEncoding giving different results

JavaScript function:

var s="ì";
var e=encodeURIComponent(s);
document.write(e);

Exspected result: %C3%AC Result: %C3%AC

PHP function:

$s = "ì";
echo $e = urlencode($s);

Exspected result: %C3%AC Result: %EC

What am I doing wrong?

Upvotes: 0

Views: 271

Answers (1)

Pekka
Pekka

Reputation: 449425

Your PHP file is likely encoded in a single-byte encoding, say ISO-8859-1. When you type in a non-ASCII character, it will use that encoding's representation of the character. You are expecting a UTF-8 (multi-byte) result.

To get that result, encode your PHP file as a UTF-8 file. Usually, your IDE will have an option for this; often in the "Save As..." dialog.

Alternatively, if you can't or don't want to change your file's encoding, you can do

echo $e = urlencode(utf8_encode($s));

Upvotes: 1

Related Questions