php 如何解码 & 来自 URL 以便 header() 工作(urldecode 不工作)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11723641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:57:18  来源:igfitidea点击:

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

phpheaderdecodingurldecode

提问by ProgrammerGirl

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

我有以下网址: $url = 'http://mysite.com/?p=welcome&x=1&y=2';

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

我需要解码它,以便header("Location: $url");实际工作。

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

但是, usingurldecode($url)不起作用,因为它没有解码&-> &,因此浏览器被重定向到http://mysite.com/?p=welcome&x=1&y=2失败。

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

我需要它来解码,使其看起来像: http://mysite.com/?p=welcome&x=1&y=2

How do I do that?

我怎么做?

回答by Esailija

Try with htmlspecialchars_decode

试试 htmlspecialchars_decode

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

回答by CodeCaster

&isn't URL encoded (aka percent-encoding), but is an HTML entity. URL-encoded it would look like %26.

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

Use html_entity_decode().

使用html_entity_decode().

回答by Matteo Tassinari

I'd try this:

我会试试这个:

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

taken from http://php.net/manual/en/function.html-entity-decode.php

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

I'd do so 'cause your $urlis not url-encoded but html-encoded, with html special characters replaced with the corresponding html entity.

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

回答by Matt

&isn't url-encoded. You need use htmlspecialchars_decode()http://php.net/manual/en/function.htmlspecialchars-decode.php

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