Current Location: Home> Function Categories> htmlentities

htmlentities

Convert all applicable characters to HTML entities - Convert characters to HTML escape characters
Name:htmlentities
Category:String
Programming Language:php
One-line Description:Convert characters to HTML entities.

Definition and usage

htmlentities() function converts characters into HTML entities.

Tip: To convert HTML entities back to characters, use html_entity_decode() function.

Tip: Please use get_html_translation_table() function to return the translation table used by htmlentities() .

Example

Example 1

Convert characters to HTML entities:

 <?php
$str = "<? W3S?h????>" ;
echo htmlentities ( $str ) ;
?>

The HTML output of the above code is as follows (see the source code):

 < ! DOCTYPE html >
< html >
< body >
< ? W3S ?h°°|§ >
< / body >
< / html >

The browser output of the above code:

 <? W3S ? h ?? ? ?>

Try it yourself

Example 2

Convert characters to HTML entities:

 <?php
$str = "Bill & 'Steve'" ;
echo htmlentities ( $str , ENT_COMPAT ) ; // Convert only double quotes
echo "<br>" ;
echo htmlentities ( $str , ENT_QUOTES ) ; // Convert double and single quotes
echo "<br>" ;
echo htmlentities ( $str , ENT_NOQUOTES ) ; // No quotation marks are converted
?>

The HTML output of the above code is as follows (see the source code):

 < ! DOCTYPE html >
< html >
< body >
Bill & 'Steve' < br >
Bill & & #039;Tarzan&#039;<br>
Bill & 'Steve'

< / body >
< / html >

The browser output of the above code:

 Bill & 'Steve'
Bill & 'Steve'
Bill & 'Steve'

Try it yourself

Example 3

By using the Western European character set, convert some characters into HTML entities:

 <?php
$str = "My name is ?yvind ?sane. I'm Norwegian." ;
echo htmlentities ( $str , ENT_QUOTES , "ISO-8859-1" ) ; 
// Will only convert double quotes (not single quotes), and uses the character-set Western European
?>

The HTML output of the above code is as follows (see the source code):

 < ! DOCTYPE html >
< html >
< body >
My name is ?yvind ?sane . I 'm Norwegian .
< / body >
< / html >

The browser output of the above code:

 My name is ?yvind ?sane. I'm Norwegian.

Try it yourself

grammar

 htmlentities ( string , flags , character - set , double_encode )
parameter describe
string Required. Specifies the string to be converted.
flags

Optional. Specifies how to deal with quotes, invalid encodings, and which document type to use.

Available quote types:

  • ENT_COMPAT - Default. Encode only double quotes.
  • ENT_QUOTES - Encoded double and single quotes.
  • ENT_NOQUOTES - No quotation marks are encoded.

Invalid encoding:

  • ENT_IGNORE - Ignore invalid encoding instead of having the function return an empty string. It should be avoided as it may have a security impact.
  • ENT_SUBSTITUTE - Replaces invalid encodings with a specified character with Unicode substitution U+FFFD (UTF-8) or &#FFFD; instead of returning an empty string.
  • ENT_DISALLOWED - Replace invalid code points in the specified document type with Unicode substitution characters U+FFFD (UTF-8) or &#FFFD;.

Additional flags for the document type used:

  • ENT_HTML401 - Default. Process code as HTML 4.01.
  • ENT_HTML5 - Process code as HTML 5.
  • ENT_XML1 - Process code as XML 1.
  • ENT_XHTML - Process code as XHTML.
character-set

Optional. A string that specifies the character set to be used.

Allowed values:

  • UTF-8 - Default. ASCII compatible with multi-byte 8-bit Unicode
  • ISO-8859-1 - Western Europe
  • ISO-8859-15 - Western Europe (added to the Euro symbol + missing French and Finnish letters in ISO-8859-1)
  • cp866 - DOS-specific Cyrillic character 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 - Big5 with Hong Kong expansion
  • Shift_JIS - Japanese
  • EUC-JP - Japanese
  • MacRoman - Character set used by Mac operating system

Note: In versions prior to PHP 5.4, unrecognized character sets will be ignored and replaced by ISO-8859-1. Since PHP 5.4, unrecognized character sets will be ignored and replaced by UTF-8.

double_encode

Optional. Boolean value, specifying whether to encode an existing HTML entity.

  • TRUE - Default. Each entity will be converted.
  • FALSE - The HTML entity that already exists is not encoded.
Similar Functions
Popular Articles