htmlspecialchars
may be used:
-
When there is no need to encode all characters which have their HTML equivalents.
If you know that the page encoding match the text special symbols, why would you use
htmlentities
?htmlspecialchars
is much straightforward, and produce less code to send to the client.For example:
echo htmlentities('<Il était une fois un être>.'); // Output: <Il était une fois un être>. // ^^^^^^^^ ^^^^^^^ echo htmlspecialchars('<Il était une fois un être>.'); // Output: <Il était une fois un être>. // ^ ^
The second one is shorter, and does not cause any problems if ISO-8859-1 charset is set.
-
When the data will be processed not only through a browser (to avoid decoding HTML entities),
-
If the output is XML (see the answer by Artefacto).