In-depth analysis of php htmlspecialchars () and shtmlspecialchars () Functions

Source: Internet
Author: User

Definition and usage
The htmlspecialchars () function converts some predefined characters into HTML objects.

The predefined characters are:
• & (And number) become &
• "(Double quotation marks)"
• '(Single quotes)'
• <(Less than) becomes <
•> (Greater than) become>
Syntax
Htmlspecialchars (string, quotestyle, character-set)

Parameters Description
String Required. Specifies the string to be converted.
Quotestyle

Optional. Specifies how to encode single quotes and double quotes.

  • ENT_COMPAT-default. Only double quotation marks are encoded.
  • ENT_QUOTES-encode double quotation marks and single quotation marks.
  • ENT_NOQUOTES-do not encode any quotation marks.
Character-set

Optional. String value that specifies the character set to be used.

  • ISO-8859-1-default. Western Europe.
  • ISO-8859-15-Western Europe (with the Euro symbol and letters in French and Finnish ).
  • UTF-8-ASCII compatible multi-byte 8-bit Unicode
  • Cp866-DOS dedicated Cyrillic character set
  • Cp1251-Windows dedicated Cyrillic character set
  • Cp1252-Western European character set for Windows
  • KOI8-R-Russian
  • GB2312-Simplified Chinese, National Standard Character Set
  • BIG5-Traditional Chinese
  • BIG5-HKSCS-Big5 Hong Kong Extension
  • Shift_JIS-Japanese
  • EUC-JP-Japanese

Tips and comments
Tip:
Unrecognized character sets will be ignored and replaced by ISO-8859-1.
Example
Copy codeThe Code is as follows:
<Html>
<Body>
<? Php
$ Str = "John & 'adams '";
Echo htmlspecialchars ($ str, ENT_COMPAT );
Echo "<br/> ";
Echo htmlspecialchars ($ str, ENT_QUOTES );
Echo "<br/> ";
Echo htmlspecialchars ($ str, ENT_NOQUOTES );
?>
</Body>
</Html>

Browser output:
Copy codeThe Code is as follows:
John & 'adams'
John & 'adams'
John & 'adams'

If you view the source code in the browser, you will see the following HTML:
Copy codeThe Code is as follows:
<Html>
<Body>
John & 'adams' <br/> John & 'adams' </body>
</Html>

========================================================== ====================================
The shtmlspecialchars () function is the opposite.

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.