tags:

views:

190

answers:

4

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ć

?

+2  A: 

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
Negative. Urldecode fails on that one badly, as it decodes only %## characters.
clops
A: 

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
gave `przysi%C4%99gam%20s%C5%82u%C5%BCy%C4%87` on my machine.
Gordon
+2  A: 

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
+1  A: 

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);
stereofrog
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
Thanks for heads up though!
clops