PHP htmlspecialchars () function example tutorial
Definition and usage
The htmlspecialchars () function converts some predefined characters into HTML entities.
The predefined characters are:
& (Symbol) become &
"(Double quotation marks)"
'(Single quotes)'
"(Less than)"
"(Greater than)"
Syntax
Htmlspecialchars (string, quotestyle, character-set)
| Parameter |
Description |
| String |
Required. Specifies the string to convert |
| Quotestyle |
|
| Character-set |
Optional. Character set specified by the string. The value is equivalent to ISO-8859-1-default. Western European International Organization for Standardization-8859-15-Western Europe (added the euro sign + letters from France and Finland lost in I SO-8 859-1) -8-a s cii compatible with multi-byte 8-bit U n icodecp 866-D OS specific Spanish character set cp 1 251-Windows specific Spanish character set cp1252-specific character set W indows Western Europe K OI8-R-Russian Personal Information Protection Policy-traditional China, mainly used in Taiwan G B2 312-simplified China, the national standard character set personal information protection policy, hong Kong Supplementary Character Set-basket and Hong Kong extension S hi ft _ JIS-Japanese eu c-JA-Japanese
|
Tips and instructions
Note: unrecognized character sets are ignored and replaced by ISO-8859-1.
Example 1
<Html> <body> <? Php $ str = "Jane & 'tarzanc'"; echo htmlspecialchars ($ str, ENT_COMPAT); echo "<br/>"; echo htmlspecialchars ($ str, ENT_QUOTES ); echo "<br/>"; echo htmlspecialchars ($ str, ENT_NOQUOTES);?> </Body>
Result.
Jane & 'tarzance' Jane & 'tarzanc'
If you select "View Source File" in the browser window, you will see the following HTML:
<Html> <body> Jane & amp; 'tarzanc' <br/> Jane & amp; & #039; Tarzan & #039; <br/> Jane & amp; 'tarzany' </body> |