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:
- double quote(")
- single quote(')
- less than(<)
- greater than(>)
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:
To avoid invaliding encoding we use:
For document type
3. characters specify which type of character set is to be used. It is also optional. Possible values are:
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.
<?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; ?>