简体   繁体   English

为什么PHP的urlencode使用不同的URL编码?

[英]Why does PHP's urlencode use different URL encoding?

The URL encoding of η is %CE%B7 . η的URL编码是%CE%B7 But in PHP I get some strange symbols when I write echo urldecode("%ce%b7"); 但在PHP中,当我写echo urldecode("%ce%b7");时,我会得到一些奇怪的符号echo urldecode("%ce%b7");

Instead, if I write echo urlencode("η"); 相反,如果我写echo urlencode("η"); then I get %26%23951%3B . 然后我得到%26%23951%3B Why can't I use %CE%B7 ? 为什么我不能使用%CE%B7

Solution

The Problem is that we use typo3. 问题是我们使用typo3。 It some how does not use unicode for internal processing. 它有些如何不使用unicode进行内部处理。 As soon as we set $TYPO3_CONF_VARS['BE']['forceCharset'] = 'utf-8'; 一旦我们设置$TYPO3_CONF_VARS['BE']['forceCharset'] = 'utf-8'; in typo3 the output of echo urldecode("%ce%b7"); 在typo3中输出echo urldecode("%ce%b7"); was correct. 是对的。

For why echo urlencode("η"); 为什么echo urlencode("η"); gives me %26%23951%3B see the answers of Joni. 给我%26%23951%3B看到Joni的答案。

urldecode("%ce%b7") produces η encoded in UTF-8 . urldecode("%ce%b7")产生以UTF-8编码的 η。 If you are viewing the output with some other encoding you may see something else. 如果您使用其他编码查看输出,您可能会看到其他内容。

On the other hand, when you decode %26%23951%3B it's true that you don't obtain η; 另一方面,当您解码%26%23951%3B时,您确实没有获得η; you obtain η 你得到η which is a HTML entity code for η. 这是η的HTML实体代码。 To decode entity codes use html_entity_decode : 要解码实体代码,请使用html_entity_decode

echo html_entity_decode('η', false, 'UTF-8'); // prints η, encoded in UTF-8

You can try the following 您可以尝试以下方法

header('Content-Type: text/html; charset=utf-8');
echo urldecode("%ce%b7"); // output : η

See Live Demo 观看现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM