Wordpressor
Wordpressor

Reputation: 7543

PHP - how to return HTML special characters code?

I'm working on function like this one:

function foo() {
   return '[ = ]';
}

But this returns:

[ @ ]

And I need:

[ = ]

Any ideas?

Upvotes: 1

Views: 957

Answers (3)

Gormit
Gormit

Reputation: 101

or use:

return urlencode('[ = ]');

Upvotes: 1

Phil
Phil

Reputation: 164809

Your function is returning the correct string.

When you view it in an HTML document, it is interpreted as an entity.

My advice would be to encode the response when you display it, eg

$foo = foo();
echo htmlspecialchars($foo, ENT_QUOTES, 'UTF-8');

Changing your function's return value will probably cause issues later on when you may not want the encoded string.

Upvotes: 1

John Flatness
John Flatness

Reputation: 33769

You could either call htmlspecialchars on your string before you output it, or you could simply replace the & in your string with a &.

Either one will make a browser show = instead of @.

Upvotes: 3

Related Questions