The difference between htmlentities and Htmlspecialchars in PHP

Source: Internet
Author: User
http://php.net/manual/zh/function.htmlspecialchars.php

Many people think that htmlentities and htmlspecialchars function is the same, are formatted HTML code, I used to think so, but today I found that is not the case.

These two functions in the format of the HTML code with English characters is basically no problem, but htmlentities to Chinese characters are not spared, so that the result is the Chinese character part into a heap of garbled characters. At that time to do the English station is not aware of this problem, and today's company's collection station is because there are non-English characters and there is a problem, I finally find out is htmlentities this function of the problem, at the same time I also found htmlspecialchars this function.

For both functions, the PHP manual is interpreted in English, where the description section of the Htmlentities function is in English:

This function was identical to Htmlspecialchars () at all ways, except with htmlentities (), all characters which has HTML C Haracter entity equivalents is translated into these entities.

From this sentence we can also see that these two functions, although the basic function of the same, but there are subtle differences in the inside. Take a closer look at the passage in the Htmlspecialchars function:

The translations performed is:

' & ' (ampersand) becomes ' & '

' ' ' (double quote) becomes ' "When Ent_noquotes was not set.

"' (single quote) becomes" ' If Ent_quotes is set.

' < ' (less than) becomes ' < '

' > ' (greater than) becomes ' > '


You can see that htmlspecialchars only translates the above HTML code, and Htmlentities translates all of the HTML code, along with its unrecognized Chinese characters.

The above describes the PHP htmlentities and htmlspecialchars differences, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.