English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP htmlentities() Function Usage and Example

   PHP String Function Manual

    The htmlentities() function is used to convert characters to HTML escape characters

Syntax

string htmlentities ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") [, bool $double_encode = true ]]] )

Definition and usage

It is used to convert all applicable characters to HTML entities

Return value

It returns the encoded string.

Parameter

Serial numberParameters and descriptions
1

string

It contains information about the input string

2

flags

Optional. Specify how to handle quotes, invalid encoding, and which document type to use.

Available quote types:

  • ENT_COMPAT - Default. Only encode double quotes.

  • ENT_QUOTES - Encode double quotes and single quotes.

  • ENT_NOQUOTES - Do not encode any quotes.

Invalid encoding:

  • ENT_IGNORE - Ignore invalid encoding instead of returning an empty string. It should be avoided as it may affect security.

  • ENT_SUBSTITUTE - 把无效的编码代替成一个指定的带有 Unicode 代替字符 U+Replace invalid code points in the specified document type with Unicode replacement character U-8Replace invalid encoding with a specified Unicode replacement character U

  • ) or &#FFFD; characters, rather than returning an empty string. - ENT_DISALLOWED+Replace invalid code points in the specified document type with Unicode replacement character U-8FFFD(UTF

) or &#FFFD;.

  • .0401 - Specify additional flags for the document type used: 4Default. As HTML1 As XML

  • .05 - ENT_HTML 5 As XML

  • As HTML1 - ENT_XML 1 As XML

  • Processing code. - ENT_XHTML

3

As XHTML processing code.

encoding

It is an optional parameter that defines the encoding used to convert characters.

  • Allowed values:-8 - UTF 8 Default. ASCII compatible multibyte

  • Western European (including Euro symbol-8859-1 - Unicode

  • Western European (including Euro symbol-8859-15 - Western European + Western European (including Euro symbol-8859-1 ISO

  • Windows专用 Cyrillic character set866 - French and Finnish letters lost in Chinese)

  • Windows专用 Cyrillic character set1251 - DOS专用 Cyrillic character set

  • Windows专用 Cyrillic character set1252 - cp

  • Windows专用 Western European character set8-KOI - R

  • Simplified Chinese, national standard character set5 - Russian

  • Traditional Chinese, mainly used in Taiwan2312 - GB

  • Simplified Chinese, national standard character set5-BIG - HKSCS5

  • With Hong Kong extension Big - JP

  • Shift_JIS-EUC - JP

  • Japanese - MacRoman

Character set used by the Mac operating systemNote: 5.4 In PHP-8859-1 Instead of. Since PHP 5.3.0, unrecognized character sets will be ignored and replaced by ISO 5.4 From PHP 5.3.0, unrecognized character sets will be ignored and replaced by UTF-8 Instead of.

4

double_encode

Optional. A boolean value that specifies whether existing HTML entities should be encoded.
  • TRUE - Default. Converts all entities.

  • FALSE - It will not encode existing HTML entities.

Online Example

Try the following example to convert characters to HTML entities

<?php
   //Convert characters to HTML entities
   $str = "PHP Function htmlentities";
   
   echo htmlentities($str);
   echo htmlentities($str, ENT_QUOTES);
?>
Test and see‹/›

Output Result-

PHP Function htmlentitiesPHP Function htmlentities

PHP String Function Manual