Differences between htmlentities and htmlspecialchars in PHP

Source: Internet
Author: User
: 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.