Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htmlentities() vs. htmlspecialchars()

Tags:

php

What are the differences between htmlspecialchars() and htmlentities(). When should I use one or the other?

like image 588
Eric Hogue Avatar asked Sep 05 '08 18:09

Eric Hogue


People also ask

What is the difference between Htmlspecialchars and HTML entities?

The most important difference between htmlentities and htmlspecialchars is the set of HTML characters that can be converted by these functions. The function htmlentities converts all the characters that are applicable to HTML entities. The function htmlspecialchars convert the special characters to HTML entities.

What is HTML entities ()?

The htmlentities() function converts characters to HTML entities. Tip: To convert HTML entities back to characters, use the html_entity_decode() function. Tip: Use the get_html_translation_table() function to return the translation table used by htmlentities().

Is Htmlspecialchars enough to prevent XSS?

are there any cases where htmlspecialchars($input, ENT_QUOTES, 'UTF-8') (converting & , " , ' , < , > to the corresponding named HTML entities) is not enough to protect against cross-site scripting when generating HTML on a web server? Yes, this is only about HTML output.

What is the purpose of the Htmlspecialchars () function?

The htmlspecialchars() function converts some predefined characters to HTML entities.


2 Answers

htmlspecialchars may be used:

  1. 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: &lt;Il &eacute;tait une fois un &ecirc;tre&gt;. //                ^^^^^^^^                 ^^^^^^^  echo htmlspecialchars('<Il était une fois un être>.'); // Output: &lt;Il était une fois un être&gt;. //                ^                 ^ 

    The second one is shorter, and does not cause any problems if ISO-8859-1 charset is set.

  2. When the data will be processed not only through a browser (to avoid decoding HTML entities),

  3. If the output is XML (see the answer by Artefacto).

like image 182
Arseni Mourzenko Avatar answered Oct 13 '22 21:10

Arseni Mourzenko


From the PHP documentation for htmlentities:

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.

From the PHP documentation for htmlspecialchars:

Certain characters have special significance in HTML, and should be represented by HTML entities if they are to preserve their meanings. This function returns a string with some of these conversions made; the translations made are those most useful for everyday web programming. If you require all HTML character entities to be translated, use htmlentities() instead.

The difference is what gets encoded. The choices are everything (entities) or "special" characters, like ampersand, double and single quotes, less than, and greater than (specialchars).

I prefer to use htmlspecialchars whenever possible.

For example:

    echo htmlentities('<Il était une fois un être>.');     // Output: &lt;Il &eacute;tait une fois un &ecirc;tre&gt;.     //                ^^^^^^^^                 ^^^^^^^      echo htmlspecialchars('<Il était une fois un être>.');     // Output: &lt;Il était une fois un être&gt;.     //                ^                 ^ 
like image 29
Thomas Owens Avatar answered Oct 13 '22 21:10

Thomas Owens