David19801
David19801

Reputation: 11448

Why is this not making a plus sign?

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.

Upvotes: 1

Views: 458

Answers (5)

Kevin Ji
Kevin Ji

Reputation: 10499

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.

Upvotes: 6

Stewie
Stewie

Reputation: 3121

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

Upvotes: 0

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25465

In html a + is +. Try

$str = 'test +';

Upvotes: 0

lonesomeday
lonesomeday

Reputation: 237985

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.

Upvotes: 2

Brian Cain
Brian Cain

Reputation: 14619

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

Upvotes: 0

Related Questions