Htmlspecialchars (), htmlspecialchars_decode ()
Definition and usage
The htmlspecialchars () function converts some predefined characters into HTML objects.
The predefined characters are:
& (And number) becomes & amp;
"(Double quotation marks) into & quot;
'(Single quotes) becomes & #039;
<(Less than) to become <;
> (Greater than) to become & gt;
Syntax
Htmlspecialchars (string, quotestyle, character-set)
Parameter description
String is required. Specifies the string to be converted.
Quotestyle is 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 is 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 a ISO-8859-1.
Example
Php code
<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:
John & 'adams'
John & 'adams'
John & 'adams'
If you view the source code in the browser, you will see the following HTML:
Php code
<Html>
<Body>
John & amp; 'adams' <br/>
John & amp; & #039; Adams & #039; <br/>
John & amp; 'adams'
</Body>
</Html>
Definition and usage
The htmlspecialchars_decode () function converts some predefined HTML entities into characters.
The HTML Entity to be decoded is:
& Amp; become & (and)
& Quot; to be "(double quotation marks)
& #039; become '(single quotes)
& Lt; become <(less)
& Gt; become> (greater)
Syntax
Htmlspecialchars_decode (string, quotestyle)
Parameter description
String is required. Specifies the string to be decoded.
Quotestyle is optional. Specifies how to decode single quotes and double quotes.
ENT_COMPAT-default. Only double quotation marks are decoded.
ENT_QUOTES-Decode double quotation marks and single quotation marks.
ENT_NOQUOTES-no quotation marks are decoded.
Example
Php code
<? Php
$ Str = "John & amp; & #039; Adams & #039 ;";
Echo htmlspecialchars_decode ($ str );
Echo "<br/> ";
Echo htmlspecialchars_decode ($ str, ENT_QUOTES );
Echo "<br/> ";
Echo htmlspecialchars_decode ($ str, ENT_NOQUOTES );
?>
Browser output:
John & 'adams'
John & 'adams'
John & 'adams'
If you view the source code in the browser, you will see the following HTML:
Php code
<Html>
<Body>
John & #039; Adams & #039; <br/>
John & 'adams' <br/>
John & #039; Adams & #039;
</Body>
</Html>