Home >Backend Development >PHP Tutorial >How to use htmlentities function
php htmlentities function is used to convert characters into HTML escape characters.
#How to use the htmlentities function?
php htmlentities() function syntax
Function: Convert characters to HTML entities
Syntax:
htmlentities(string,flags,character-set,double_encode)
Parameters:
string Required, specifies the string to be converted
flagsOptional. Specifies how to handle quotes, invalid encodings, and which document type to use. Available quote types:
ENT_COMPAT - Default. Only double quotes are encoded.
ENT_QUOTES - Encodes double and single quotes.
ENT_NOQUOTES - Do not encode any quotes.
Invalid encoding: ENT_IGNORE - Ignore invalid encodings instead of having the function return an empty string. This should be avoided as this may have an impact on security.
ENT_SUBSTITUTE - Substitutes an invalid encoding with the specified character with the Unicode substitution character U FFFD (UTF-8) or FFFD; instead of returning an empty string.
ENT_DISALLOWED - Replaces invalid code points in the specified document type with the Unicode replacement character U FFFD (UTF-8) or FFFD;. Additional flags specifying the document type to use:
ENT_HTML401 - Default. Code processed as HTML 4.01.
ENT_HTML5 - Process code as HTML 5. ENT_XML1 - Code processed as XML 1.
ENT_XHTML - Processing code as XHTML.
character-set Optional. A string specifying the character set to be used. Allowed values: UTF-8 - Default. ASCII compatible multi-byte 8-bit Unicode, ISO-8859-1 - Western Europe, ISO-8859-15 - Western Europe (adds French and Finnish letters missing from ISO-8859-1 for Euro symbol), cp866 - DOS-specific Cyrillic characters set, cp1251 - Windows-specific Cyrillic character set, cp1252 - Windows-specific Western European character set, KOI8-R - Russian, BIG5 - Traditional Chinese, mainly used in Taiwan, GB2312 - Simplified Chinese, national standard character set, BIG5-HKSCS - with Hong Kong Extended Big5, Shift_JIS - Japanese, EUC-JP - Japanese, MacRoman - Character set used by Mac operating systems. In versions prior to PHP 5.4, unrecognized character sets will be ignored and replaced by ISO-8859-1. As of PHP 5.4, unrecognized character sets are ignored and replaced by UTF-8.
double_encodeOptional. Boolean value that specifies whether to encode existing HTML entities. TRUE - Default. Each entity will be converted. FALSE - Existing HTML entities will not be encoded.
Description: Convert characters into HTML entities.
php htmlentities() function usage example:
<?php $str = "Bill& 'Steve'"; echo htmlentities($str, ENT_COMPAT); // 只转换双引号 echo "<br>"; echo htmlentities($str, ENT_QUOTES); // 转换双引号和单引号 echo "<br>"; echo htmlentities($str, ENT_NOQUOTES); // 不转换任何引号 ?>
Output:
Bill & 'Steve' Bill & 'Steve' Bill & 'Steve'
This article is an introduction to the PHP htmlentities function. I hope it will help you if you need Friends help!
The above is the detailed content of How to use htmlentities function. For more information, please follow other related articles on the PHP Chinese website!