为什么这不是加号?

I have:

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

I want it to return test +

What am I doing wrong?

NOTE: Sorry, the string cannot be modified. It's from an external source, I just need to make it replace the %2B with + signs somehow with PHP.

You didn't escape the space, and you should be using urldecode instead of html_entity_decode.

Try

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

If you wish to use html_entity_decode, use +:

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

EDIT: If you need to decode a url that you cannot change yourself, urldecode should still work fine.

Try + instead. In your example, you are using URL encoding syntax and not HTML entity syntax.

That string is encoded for a URL, not with HTML entities.

You need urldecode.

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

An HTML-encoded string would look like this: test +, because none of those characters need HTML-encoding.

In html a + is +. Try

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