htmlentities() function in PHP || PHP htmlentities function
×


htmlentities() function in PHP

0 477

PHP htmlentities() function is used to converts characters to HTML entities. It is an inbuilt function of PHP. The return type of this function is string means it returns the encoded string.

Note:

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

Syntax:

htmlentities($string,flags,characters,d_encode);

here,

1) $string is a PHP string which characters to be converted into HTML entities by this function. It is required.

2) flags used to specify the quotes and document type used by the function. It is also an optional parameter.

For quotes styles we use:

  • ENT_COMPAT
  • ENT_QUOTES
  • ENT_NOQUOTES

To avoid invaliding encoding we use:

  • ENT_IGNORE
  • ENT_SUBSTITUTE
  • ENT_DISALLOWED
  • For document type
  • ENT_HTML401
  • ENT_HTML5
  • ENT_XML1
  • ENT_XHTML

3. characters specify which type of character set is to be used. It is also optional. Possible values are:

  • UTF-8
  • ISO-8859-1
  • ISO-8859-15
  • cp866
  • cp1251
  • cp1252
  • KOI8-R
  • BIG5
  • GB2312
  • BIG5-HKSCS
  • Shift_JIS
  • EUC-JP
  • MacRoman

4. d_encode used to ensure that the available HTML entities are going to encode or not. It is optional. Although it is a Boolean value so it has two possible values

TRUE: by default it is selected and makes all characters including existing HTML entities encodable.

FALSE: already existing HTML entities will not be converted by the function.


Example:

<?php
$string = "Chemical formula for water is :'H2O'";
$a=htmlentities($string,ENT_QUOTES); // it will convert single quotes into HTML entities
echo $a;
?>

Output:

View-source Output:




Share:


Leave a Reply


Comments
    Waiting for your comments