Jump to content


This topic is now archived and is closed to further replies.


htmlentities() versus htmlspecialchars()?

Recommended Posts

I need a clear explanation about when exactly htmlentities() is more useful versus htmlspecialchars() if anyone can help.

Anyone want to take a stab at explanation?
my best guess is htmlspecialchars() only does whats needed(' " & < >) so it seems like the default choice when need to pull database values and put them into html (which needs those chars escapes). But when would you need to convert all chars to their equivalents (i know there are probably alot of scanarios but was looking for common things you see in web programming)? Someone told me encoding/decoding values into the query string, but wasnt sure.

Share this post

Link to post
Share on other sites
as far as a defining the differences between the two, i think the manual itself does that the best:
This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.

as for one example when you might want to translate all characters to their equivalents may be if you're creating an RSS feed or generating XML or XHTML through the PHP. for [i]best practices[/i], it's good to use entities rather than the symbols themselves whenever possible.

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.