texai
texai

Reputation: 3736

PHP urlencode charset encoding issue

I got this php script:

$str = "ú";
echo $str . ' -> ' . urlencode($str) . "\n" ;

Expected Result:

ú -> %FA

Reference: http://www.w3schools.com/tags/ref_urlencode.asp

Actual Result

ú -> %C3%BA

Upvotes: 0

Views: 4410

Answers (2)

hakre
hakre

Reputation: 197544

You encode the ú as UTF-8 (check the encoding of your example code), so urlencode does correctly encode it as %C3%BA.

You were more or less referring to this:

$str = "\xFA"; # ú in LATIN-1
echo $str . ' -> ' . urlencode($str) . "\n" ;

Which gives you your expected result, regardless how you encode the php-code/-file:

ú -> %FA

Demo, that site is using UTF-8 to store the source-code. If you want the output displayed as LATIN-1, this additional example signals the browser the LATIN-1 charset:

header('Content-Type: text/html; charset=latin-1');
$str = "\xFA"; # ú in LATIN-1
echo $str . ' -> ' . urlencode($str) . "\n" ;

Upvotes: 2

Jeremy Harris
Jeremy Harris

Reputation: 24549

Try this:

urlencode(utf8_decode($str));

That should give you the expected result.

Upvotes: 4

Related Questions