This article mainly introduces the definition and usage of the htmlentities() function in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.
php htmlentities() function converts characters into HTML entities. This article introduces the basic usage and examples of the php htmlentities() function to coders. Coders in need can refer to it.
Definition and Usage
htmlentities() function converts characters into HTML entities.
Tip: To convert HTML entities back to characters, use the html_entity_decode() function.
Tip: Please use the get_html_translation_table() function to return the translation table used by htmlentities().
Grammar
htmlentities(string,flags,character-set,double_encode)
Parameters | Description |
---|---|
string | Required. Specifies the string to be converted. |
flags | Optional. Specifies how to handle quotes, invalid encodings, and which document type to use. Available quote types:
Invalid encoding:
Additional flags specifying the document type to use:
|
character-set | Optional. A string specifying the character set to be used. Allowed values:
|
Optional. Boolean value that specifies whether to encode existing HTML entities.
|
Example 1
Convert characters to HTML entities:
"; echo htmlentities($str, ENT_QUOTES); // 转换双引号和单引号 echo "
"; echo htmlentities($str, ENT_NOQUOTES); // 不转换任何引号 ?>
The HTML output of the above code is as follows (view source code):
Bill & 'Steve'
Bill & 'Tarzan'
Bill & 'Steve'
Browser output of the above code:
Bill & 'Steve' Bill & 'Steve' Bill & 'Steve'
Example 2
Convert some characters into HTML entities by using the Western European character set:
The HTML output of the above code is as follows (view source code):
My name is Øyvind Åsane. I'm Norwegian.
The browser output of the above code:
My name is ?yvind ?sane. I'm Norwegian.
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
PHP MariaDB database operation basic skills memo summary
phpAchieving manipulation of mysqli database Method
PHP Method to access Alipay’s instant payment function
Return the converted string.If string contains an invalid encoding, an empty string is returned unless the ENT_IGNORE or ENT_SUBSTITUTE flag is set. | |
4 | |
In In PHP 5, the default value of the character-setparameter is changed to UTF-8. In PHP 5.4, new: ENT_SUBSTITUTE, ENT_DISALLOWED, ENT_HTML401, ENT_HTML5, ENT_XML1 and ENT_XHTML.In PHP 5.3, ENT_IGNORE is added.In PHP 5.2.3, thedouble_encodeparameter was added. In PHP 4.1, thecharacter-setparameter was added. |
The above is the detailed content of The definition and usage of htmlentities() function in php. For more information, please follow other related articles on the PHP Chinese website!