English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The htmlspecialchars() function is used to convert special characters to HTML entities.
string htmlspecialchars(string $string[, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset")[, bool $double_encode = true]])
Used to convert special characters to HTML entities
It returns the converted string (string).
If the specified encoding 'encoding' contains invalid code unit sequences in 'string', and no ENT_IGNORE or ENT_SUBSTITUTE flags are set, an empty string will be returned.
The predefined characters are:
& (ampersand) becomes &
" (double quote) becomes "
' (single quote) becomes '
< (less than) becomes <
> (greater than) becomes >
Note:To convert special HTML entities back to characters, use htmlspecialchars_decode() Function.
Serial number | Parameters and descriptions |
---|---|
1 | string Required. It contains information about the input string |
2 | flags Optional. Specify how to handle quotes, invalid encodings, and which document type to use.Available quote types:
Invalid encoding:
Specify additional flags for the document type used.
|
3 | encoding It is an optional parameter that defines the encoding used when converting characters. Allowed values:
Note:In PHP 5.4 earlier versions, unrecognized character sets are ignored and replaced by ISO-8859-1 Replace with. Since PHP 5.4 Starting with PHP-8 Replace with. |
4 | double_encode A boolean value that specifies whether existing HTML entities should be encoded.
|
Try the following example, converting predefined characters to HTML entities:
<?php //Convert predefined characters to HTML entities, encoding double quotes and single quotes $input = htmlspecialchars("<a href='https://www.oldtoolbag.com'>w3codebox</a>" echo $input; ?>Test and see‹/›
Output Result-
<a href='https://www.oldtoolbag.com'>w3codebox</a>