PHP htmlspecialchars() Function

PHP htmlspecialchars() Function converts some predefined characters to HTML entities.

A predefined characters are given as below.

 

  1. & (ampersand) will be converted to &
  2. " (double quote) will be converted to "
  3. ' (single quote) will be converted to '
  4. < (less than) will be converted to &lt;
  5. > (greater than) will be converted to &gt;

Syntax :

htmlspecialchars(string,flags,character-set,double_encode);

Parameter,

Parameter Description
string Required. It is input string.
flags

Optional. It specifies how to handle quotes, invalid encoding and the used document type.The default is ENT_COMPAT | ENT_HTML401.
Flags for quotes are as below,

  1. ENT_COMPAT - Encodes only double quotes
  2. ENT_QUOTES - Encodes double and single quotes
  3. ENT_NOQUOTES - Does not encode any quotes

Flags for invalid encoding are as below,

  1. ENT_IGNORE  - It ignores invalid encoding
  2. ENT_SUBSTITUTE  - It replaces invalid code unit sequences with a Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD;
  3. ENT_DISALLOWED  - It replaces code points that are invalid in the specified doctype with a Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD;

Flags for used doctype as below,

  1. ENT_HTML401 - Default. It handles code as HTML 4.01
  2. ENT_HTML5 - It handles code as HTML 5
  3. ENT_XML1 - It handles code as XML 1
  4. ENT_XHTML - It handles code as XHTML
character-set

Optional. It specifies which character-set to use for string.

Allowed character-set are as below.

  1. UTF-8
  2. ISO-8859-1
  3. ISO-8859-15
  4. GB2312
  5. cp1251
  6. cp1252
double_encode

Optional.A boolean value that specifies whether to encode existing html entities or not.

  1. TRUE - Default. It Will convert everything.
  2. FALSE - It Will not encode existing html entities.

 

Let's see below example to understand php htmlspecialchars() Function in details.

HTML Output for below example in view source as below,

The browser output of the below example as below.

Converts ampersand to HTML entities 
Learn PHP &amp; HTML

Converts double quote to HTML entities 
Online tutorial is available for &quot;php&quot;

Converts single quote to HTML entities
Online tutorial is available for &#039php&#039

Converts less than and greater than to HTML entities 
&lt;a href='https://www.aryatechno.com/blog.html'&gt;Learn online tutorials&lt;/a&gt;  

Example :

<br><b>The browser output of the above code as below.</b>
<?php
echo "<br><br><b>Converts ampersand to HTML entities </b>";
$str = "Learn PHP & HTML";
echo "<br>".htmlentities($str);
echo "<br><br><b>Converts double quote to HTML entities </b>";
$str = 'Online tutorial is available for "php"';
echo "<br>".htmlentities($str);
echo "<br><br><b>Converts single quote to HTML entities </b>";
$str = "Online tutorial is available for 'php'";
echo "<br>".htmlentities($str);
echo "<br><br><b>Converts less than and greater than to HTML entities </b>";
$str = "<a href='https://www.aryatechno.com/blog.html'>Learn online tutorials</a>";
echo "<br>".htmlentities($str);

?>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

79545