htmlspecialchars() function in PHP
×


htmlspecialchars() function in PHP

1849

htmlspecialchars() function of PHP is used convert some predefined characters to HTML entities. It is an inbuilt function of PHP and these predefined characters are:

  • ampersand(&)
  • double quote(")
  • single quote(')
  • less than(<)
  • greater than(>)

Syntax:

htmlspecialchars($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 = "Ram says to his mother: 'Please give me water'";
$a=htmlspecialchars($string,ENT_QUOTES); // it will convert single quotes to HTML entities
echo $a;
?>

Output:

View-source result:



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments