What is the difference between htmlentities and htmlspecialchars in php




















Difference between htmlentities and htmlspecialchars function: The only difference between these function is:. This article is attributed to GeeksforGeeks. It is combination of one or two flags, which tells how to handle quotes. If encoding is not given then it is converted according to PHP default version.

This is counter-intuitive and serves no practical purpose because the HTML spec actually has the opposite. Use htmlspecialchars to filter text input values for html input tags. Use htmlentities to filter the same data values for most other kinds of html tags, i.

This may seem obvious, but it caused me some frustration. I was recently exploring some code when I saw this being used to make data safe for "SQL". This function should not be used to make data SQL safe although to prevent phishing it is perfectly good. Turning on magic quotes might fix it but you should not rely on magic quotes, in fact you should never use it and fix the code instead.

Even if magic quotes were used there would be the problem of allowing usernames longer than the limit and having some really weird usernames given they are to be used outside of html, this just provide a front end for registering to another system using mysql.

Of course using it on the output wouldn;t cause that problem. This function should simply never be used in such a fashion. I hope this will prevent newbies using this function incorrectly as they apparently do. Since PHP 5. Be aware of the encoding of your source files!!! Some of the suggestions here make reference to workarounds where you hard-code an encoding.

Latin-1 or WIN Connect and share knowledge within a single location that is structured and easy to search. What are the differences between htmlspecialchars and htmlentities.

When should I use one or the other? If you know that the page encoding match the text special symbols, why would you use htmlentities? If the output is XML see the answer by Artefacto. 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 :. 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.

However, if you also have additional characters that are Unicode or uncommon symbols in your text then you should use htmlentities to ensure they show up properly in your HTML page. This leaves your string more human-readable than it would be if you used htmlentities to encode absolutely everything that has an encoding. You can check the following code for more information about what's htmlentities and htmlspecialchars:.

You probably want to use some Unicode character encoding, for example UTF-8 , and htmlspecialchars. Because there isn't any need to generate "HTML entities" for "all [the] applicable characters" that is what htmlentities does according to the documentation if it's already in your character set.

The differences between htmlspecialchars and htmlentities is very small. Lets see some examples:. So they will be not converted to HTML entities by htmlspecialchars function as shown in the below example. 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.



0コメント

  • 1000 / 1000