36

I am trying to decode this URL string using PHP's urldecode function:

urldecode("Ant%C3%B4nio+Carlos+Jobim");

This is supposed to output...

'Antônio Carlos Jobim'

...but instead is ouptutting this

'Antônio Carlos Jobim'

I've tested the string in a JS-based online decoder with great success, but can't seem to do this operation server side. Any ideas?

nalply
  • 26,770
  • 15
  • 78
  • 101
Brandon Jackson
  • 785
  • 1
  • 5
  • 10

6 Answers6

70

Your string is also UTF-8 encoded. This will work:

echo utf8_decode(urldecode("Ant%C3%B4nio+Carlos+Jobim"));

Output: "Antônio Carlos Jobim".

Kristoffer Bohmann
  • 3,986
  • 3
  • 28
  • 35
14

Actually, you get the desired output, but it is not interpreted as UTF-8. If this is on an HTTP application, you should send a header or a meta tag (or both) which would tell the client to use UTF-8.

Edit: for example:

// replace text/html with the content type you're using
header('Content-Type: text/html; charset=UTF-8');
Ignas R
  • 3,314
  • 2
  • 23
  • 27
3

another option is:

<?php
$smthing = 'http%3A%2F%2Fmysite.com';
$smthing = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($smthing)); 
$smthing = html_entity_decode($smthing,null,'UTF-8');
echo $smthing;
?>

output becomes : http://mysite.com

T.Todua
  • 53,146
  • 19
  • 236
  • 237
  • This works. Only urldecode() won't decode whole URL with query params. Problem is that your solution thinks too long :/ – temo Apr 15 '21 at 11:00
3

when I do

<?php
echo urldecode("Ant%C3%B4nio+Carlos+Jobim");
?>

Its display correctly in my browser like

Antônio Carlos Jobim

I have tested with XAMPP

YOU
  • 120,166
  • 34
  • 186
  • 219
1

Are you also using htmlenteties before echoing it to the page? When I just tested your code it worked fine with just the urldecode("Ant%C3%B4nio+Carlos+Jobim"); part, but when I ran it through htmlentities I got the same output as you did.

It seems to be a problem with the UTF-8 characters and how PHP handles the htmlentities function.

Doug Neiner
  • 65,509
  • 13
  • 109
  • 118
  • 1
    It works correctly if you specify the right encoding as the `$charset` parameter. And anyway, you should be using `htmlspecialchars`, not `htmlentities`, if all you want to do is protect from XSS. – Ignas R Nov 18 '09 at 15:48
-2

first you have to decode in urldecoder function "urldecoder()" and then use utf decoder function "utf_decoder()"