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.