0

Is ther any built in function in PHP to decode

przysi%25C4%2599gam%2520s%25C5%2582u%25C5%25BCy%25C4%2587

into

przysięgam służyć

?

clops
  • 5,085
  • 6
  • 39
  • 53
  • 1
    Are you sure, that the program generating these strings knows what it's doing? If it is an UTF-8 url encoded string, it should read `przysi%25%C4%25%99gam...`, that is, with both bytes url-encoded. – Boldewyn Apr 01 '10 at 11:10

4 Answers4

2

urldecode — Decodes URL-encoded string

But I think that won't work on multibyte strings. See the comments on the manual page for possible userland workarounds and also http://www.zend.com//code/codex.php?ozid=839&single=1

Gordon
  • 312,688
  • 75
  • 539
  • 559
2

My guess is that you have issues with urldecode and multibyte characters. Urldecode can only decode 8 bit characters and your string contains multibyte characters.

Check the urldecode manual page comments for some solutions.

ChrisR
  • 14,370
  • 16
  • 70
  • 107
2

Ok, Gordon and Manos, you're both right (and both wrong ;)

It's just normal 'urldecode', but applied twice

$a = "przysi%25C4%2599gam%2520s%25C5%2582u%25C5%25BCy%25C4%2587";
$b = urldecode(urldecode($a));
var_dump($b);
user187291
  • 53,363
  • 19
  • 95
  • 127
  • This is interesting, because the String is being posted a mortal JS script using encodeURLEntity() -- no idea why the stuff is encoded twice then – clops Apr 01 '10 at 12:03
0

Have you tried this:

    function utf8_urldecode($str) {
      $str = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($str));
      return html_entity_decode($str,null,'UTF-8');;
  }

Taken from http://php.net/manual/en/function.urldecode.php

Edit: Your initial string is messed up. Did you urlencode it twice? Cause utf8_urldecode(utf8_urldecode($encoded string)) gives the correct result.

Manos Dilaverakis
  • 5,849
  • 4
  • 31
  • 57