-1

Instead of doing this

urldecode(urldecode(urldecode(curl_exec($ch))));

The output from curl_exec is encoded more than once, is there a way I can decode it fully with only one call to a function?

I just wrote this function and it works perfectly

function decodeSource($encodedSource){
    while($encodedSource != urldecode($encodedSource)){
        $encodedSource = urldecode($encodedSource);
    }
return $encodedSource;
}
CLUEL3SS
  • 223
  • 1
  • 3
  • 10
  • "The output from curl_exec is encoded more than once" --- by whom? – zerkms Jun 04 '12 at 01:57
  • 2
    And ... how would such a decode function know how many times to iterate its urldecode()? – ghoti Jun 04 '12 at 01:59
  • I am loading a webpage using the cURL extension, curl_exec() returns the source of the webpage, and the webpage is encoded, and has \u0026, \u0026amp; as well that I would like to have decoded – CLUEL3SS Jun 04 '12 at 02:01
  • That probably means that the page being fetched has `urlencode`d the content... So just remove whatever is encoding the content multiple times and it shouldn't be an issue. – ian.aldrighetti Jun 04 '12 at 02:04
  • ghoti, thats what I was curious about as well – CLUEL3SS Jun 04 '12 at 02:04
  • \u0026 is unicode, looks like whatever is being returned is going through something other than `urlencode`. btw, calling `urlencode` multiple times on a url has no effect, it can't encode it more. – Crisp Jun 04 '12 at 02:27

1 Answers1

1

What about using your own function?

function multiurldecode($url, $count=1) {
  for($i=1; $i<=$count; $i++)
    $url=urldecode($url);
  return($url);
}

This is off the top of my head, untested....

UPDATE per comment:

Not a bad idea at all! Here it is in code.

function multiurldecode($url, $count=1) {
  if ($count==0) {
    for( $last=urldecode($url); $last!=$url; $url=urldecode($url) )
      $last=$url;
  } else {
    for ($i=1; $i<=$count; $i++)
      $url=urldecode($url);
  }
  return($url);
}

Again, I haven't tested this, but it runs fine in my head. :) The idea here is that if you provide a count of zero, the for loop will keep running until the decoded URL is the same as its source.

ghoti
  • 45,319
  • 8
  • 65
  • 104
  • 1
    You could add a check to see if the string is different than the decoded string... And then stop when they are the same. – Brian Adkins Jun 04 '12 at 02:05
  • 1
    Yes, but that might go too far. What if the string you *want* is the second last urlencode? I think we need more information from the OP. – ghoti Jun 04 '12 at 02:09
  • edited the original post with a function that I wrote, works just how I needed it too, ty all – CLUEL3SS Jun 04 '12 at 02:34