为什么这不是加号?

时间:2011-12-20 19:14:12

标签: php

我有:

$str = 'test%2B';
echo html_entity_decode($str);

我希望它返回test +

我做错了什么?

注意:抱歉,无法修改字符串。它来自外部源,我只需要用PHP以某种方式用+符号替换%2B。

5 个答案:

答案 0 :(得分:5)

你没有逃离这个空间,你应该使用urldecode而不是html_entity_decode

尝试

$str = 'test%20%2B';
echo urldecode($str); // test +

如果您想使用html_entity_decode,请使用+

$str = 'test +';
echo html_entity_decode($str); // test +

编辑:如果您需要解码一个无法自行更改的网址,urldecode仍然可以正常工作。

答案 1 :(得分:2)

该字符串是为URL编码的,而不是HTML实体。

您需要urldecode

echo urldecode($str); // "test +"

HTML编码的字符串如下所示:test +,因为这些字符都不需要HTML编码。

答案 2 :(得分:0)

请尝试使用+。在您的示例中,您使用的是URL编码语法,而不是HTML实体语法。

答案 3 :(得分:0)

在html中,++。尝试

$str = 'test +';

答案 4 :(得分:0)

$str = "test %2B";
echo urldecode($str);