简体   繁体   中英

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?

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 @ .

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.

或使用:

return urlencode('[ = ]');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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