How to Decode the & from a URL so that header() works (urldecode not working)

ProgrammerGirl picture ProgrammerGirl · Jul 30, 2012 · Viewed 21.8k times · Source

I have the following URL: $url = 'http://mysite.com/?p=welcome&x=1&y=2';

I need to decode it so that header("Location: $url"); actually works.

However, using urldecode($url) is not working because it's not decoding the & -> & and so the browser is getting redirected to http://mysite.com/?p=welcome&x=1&y=2 which fails.

I need it to decode so that it looks like: http://mysite.com/?p=welcome&x=1&y=2

How do I do that?

Answer

Esailija picture Esailija · Jul 30, 2012

Try with htmlspecialchars_decode

echo htmlspecialchars_decode('http://mysite.com/?p=welcome&x=1&y=2');
//"http://mysite.com/?p=welcome&x=1&y=2"