14

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?

4

4 回答 4

30

尝试htmlspecialchars_decode

echo htmlspecialchars_decode('http://mysite.com/?p=welcome&x=1&y=2');
//"http://mysite.com/?p=welcome&x=1&y=2"
于 2012-07-30T14:34:48.970 回答
6

&不是 URL 编码(又名percent-encoding),而是一个 HTML 实体。URL 编码的它看起来像%26.

使用html_entity_decode().

于 2012-07-30T14:34:57.483 回答
4

&不是 url 编码的。您需要使用htmlspecialchars_decode() http://php.net/manual/en/function.htmlspecialchars-decode.php

于 2012-07-30T14:35:06.407 回答
4

我会试试这个:

$url = html_entity_decode($url);
header("Location: $url");

取自http://php.net/manual/en/function.html-entity-decode.php

我会这样做,因为您$url的不是 url 编码而是 html 编码,用相应的 html 实体替换了 html 特殊字符。

于 2012-07-30T14:35:40.360 回答