The PHP htmlentities () function converts characters to HTML entities, this article introduces the PHP htmlentities () function Basic use method and the example introduction, needs the code agriculture may refer to.
Definitions and usage
The Htmlentities () function converts a character to an HTML entity.
Tip: To convert an HTML entity back to a character, use the Html_entity_decode () function.
Tip: 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 |
Necessary. Specify the string to convert. |
Flags |
is optional. Specify how to handle quotes, invalid encodings, and which type of document to use. The type of quotation mark available for :
- ent_compat-Default. Only double quotes are encoded.
- ent_quotes-encodes double quotes and single quotes.
- ent_noquotes-does not encode any quotes.
Invalid encoding:
- Ent_ignore-ignores invalid encodings rather than having the function return an empty string. Should be avoided as far as possible, as this may have an impact on security.
- Ent_substitute-Replaces an invalid encoding with a specified character with a Unicode replacement character u+fffd (UTF-8) or & #FFFD, rather than returning an empty string.
- ent_disallowed-Replaces the invalid code point in the specified document type with a Unicode replacement character u+fffd (UTF-8) or & #FFFD;. The
Provides additional flags for document types used:
- ent_html401-Default. Process code as HTML 4.01.
- ENT_HTML5-handles code as HTML 5.
- ENT_XML1-Processes code as XML 1.
- ent_xhtml-processing code as XHTML.
|
Character-set |
Optional. A string that sets the character set to be used. Allowed values:
- UTF-8-Default. ASCII-compatible multibyte 8-bit Unicode
- Iso-8859-1-Western Europe
- Iso-8859-15-Western Europe (Add Euro sign + iso-8859-1 missing French and Finnish letters)
- Cp866-dos dedicated Cyrillic Character Set
- Cp1251-windows dedicated Cyrillic Character Set
- Cp1252-windows Special Western European character set
- Koi8-r-Russian
- BIG5-Traditional Chinese, mainly used in Taiwan
- GB2312-Simplified Chinese, national standard character set
- Big5-hkscs-Big5 with Hong Kong expansion
- Shift_JIS-Japanese
- EUC-JP-Japanese
- Macroman-mac the character set used by the operating system
Note: In the previous version of PHP 5.4, the character set that could not be identified will be ignored and replaced by Iso-8859-1. Since PHP 5.4, the character set that cannot be recognized will be ignored and replaced by UTF-8. |
Double_encode |
Optional. Boolean value that stipulates whether an existing HTML entity is encoded.
- TRUE-Default. Each entity will be converted.
- FALSE-The HTML entity that already exists is not encoded.
|
Technical details
return value: |
Returns the converted string. If string contains an invalid encoding, an empty string is returned unless the Ent_ignore or Ent_substitute flag is set. |
PHP version: |
4 + |
Update log: |
In PHP 5, the default value for thecharacter-set parameter is changed to UTF-8. In PHP 5.4, add: 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, new double_encode parameters are added. In PHP 4.1, new character-set parameters are added. |
Instance Example 1
Converts a character to an HTML entity:
<?php
$str = "Bill & ' Steve";
Echo htmlentities ($str, Ent_compat); Convert only double quotes
echo "<br>";
Echo htmlentities ($str, ent_quotes); Convert double quotes and single quotes
echo "<br>";
Echo htmlentities ($str, ent_noquotes); Do not convert any quotes
?>
The HTML output of the above code is as follows (view source code):
Browser output for the above code:
Bill & ' Steve '
Bill & Steve '
Bill & ' Steve '
Example 2
Converts some characters to an HTML entity by using the Western European character set:
<?php
$str = "My name is? Yvind sane." I ' m Norwegian. ';
Echo htmlentities ($str, Ent_quotes, "iso-8859-1");
Would only convert double quotes (not single quotes) and uses the Character-set Western European?>
The HTML output of the above code is as follows (view source code):
Browser output for the above code:
My name is? Yvind Sane. I ' m Norwegian.
The above PHP htmlentities () function definition and usage is small series to share all the content, hope to give everyone a reference, also hope that we support cloud habitat community.