Php htmlentities
From w3cyberlearnings
Contents |
PHP function htmlentities
This function converts characters to HTML entities. Great HTML entities by using htmlentities function.
Syntax htmlentities
- string: string input for decode.
- quotestyle:
- ENT_COMPAT: Will convert double-quotes and leave single-quotes alone.
- ENT_QUOTES: Will convert both double and single quotes.
- ENT_NOQUOTES: Will leave both double and single quotes unconverted.
- ENT_HTML401: Handle code as HTML 4.01.
- ENT_XML1: Handle code as XML 1.
- ENT_XHTML: Handle code as XHTML.
- ENT_HTML5: Handle code as HTML 5.
- characterset:
- ISO-8859-1: Default. Western European
- ISO-8859-15: Western European (adds the Euro sign + French and Finnish letters missing in ISO-8859-1)
- UTF-8: ASCII compatible multi-byte 8-bit Unicode
- cp866: DOS-specific Cyrillic charset
- cp1251: Windows-specific Cyrillic charset
- cp1252: Windows specific charset for Western European
- KOI8-R: Russian
- BIG5: Traditional Chinese, mainly used in Taiwan
- GB2312: Simplified Chinese, national standard character set
- BIG5-HKSCS: Big5 with Hong Kong extensions
- Shift_JIS: Japanese
- EUC-JP: Japanese
htmlentities(string,quotestyle,characterset);
Example 1
<?php $str = '"Jim" & John: \'Jim\' >= John'; echo 'test1:' . htmlentities($str); echo '<br/>'; echo 'test2:' . htmlentities($str, ENT_COMPAT); // encode only double quotes echo '<br/>'; echo 'test3:' . htmlentities($str, ENT_QUOTES); // encode double and single quotes echo '<br/>'; echo 'test4:' . htmlentities($str, ENT_NOQUOTES); // do not encode any quotes echo '<br/>'; ?>
Output
Get the same result when you are viewing on the browser.
test1:"Jim" & John: 'Jim' >= John test2:"Jim" & John: 'Jim' >= John test3:"Jim" & John: 'Jim' >= John test4:"Jim" & John: 'Jim' >= John
HTML Source
test1:"Jim" & John: 'Jim' >= John<br/> test2:"Jim" & John: 'Jim' >= John<br/> test3:"Jim" & John: 'Jim' >= John<br/> test4:"Jim" & John: 'Jim' >= John<br/>