0

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
9
  • What is your issue? You think the example is lying? Commented Feb 15, 2012 at 14:23
  • 2
    Don't trust anything w3schools tells you Commented Feb 15, 2012 at 14:24
  • I expect thar urlencode('ú') returns %FA instead of %C3%BA Commented Feb 15, 2012 at 14:25
  • What char set is that letter in? As well, w3fools Commented Feb 15, 2012 at 14:25
  • 1
    @texai: It does, but not for every ú - computers differ how any character is represented, depending on the charset encoding. For LATIN-1 it does (%FA), for UTF-8 it does not (%C3%BA). Commented Feb 15, 2012 at 14:27

2 Answers 2

4

Try this:

urlencode(utf8_decode($str));

That should give you the expected result.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.