: This article mainly introduces the differences between htmlentities and htmlspecialchars in PHP. For more information about PHP tutorials, see. Http://php.net/manual/zh/function.htmlspecialchars.php
Many people think that htmlentities and htmlspecialchars have the same functions and are used to format html code. I used to think so, but today I found this is not the case.
These two functions are basically no problem when formatting html code with English characters, but htmlentities does not let go of Chinese characters. The result is that the Chinese character section turns into a bunch of garbled characters. At that time, I was not aware of this problem when I was an English site. But today, a favorite site of the company has encountered a problem due to non-English characters, I finally found out the problem with the htmlentities function, and I also found the htmlspecialchars function.
These two functions are explained in English in the php Manual. the description section of the htmlentities function is as follows:
This function is identical to htmlspecialchars () in all ways, except t with htmlentities (), all characters which have HTML character entity equivalents are translated into these entities.
From this sentence, we can also see that although these two functions have similar basic functions, there are still slight differences in them. Let's take a closer look at a paragraph in the htmlspecialchars function:
The translations saved Med are:
'&' (Ampersand) becomes '&'
'"' (Double quote) becomes '" 'When ENT_NOQUOTES is not set.
"'(Single quote) becomes"' only when ENT_QUOTES is set.
'<' (Less than) becomes' <'
'>' (Greater than) becomes '>'
We can understand that htmlspecialchars only converts the preceding html code, while htmlentities converts all the html code, along with the unrecognized Chinese characters in it.
The above introduces the differences between htmlentities and htmlspecialchars in PHP, including some content, and hope to help those who are interested in PHP tutorials.