Convert the predefined HTML entities "<" (less than) and ">" (greater than) to characters:
<?php $str = " This is some <b>bold</b> text. " ; echo htmlspecialchars_decode ( $str ) ; ?>The HTML output of the above code is as follows (view source code):
< ! DOCTYPE html > <html> < body > This is some < b > bold </ b > text. </ body > </html>The browser output of the above code is as follows:
This is some bold text.The htmlspecialchars_decode() function converts some predefined HTML entities into characters.
The HTML entities that will be decoded are:
& decodes into & (ampersand)
" decodes to " (double quote)
' decodes to ' (single quote)
< decodes to < (less than)
> decodes to > (greater than)
The htmlspecialchars_decode() function is the inverse function of the htmlspecialchars() function.
htmlspecialchars_decode( string,flags )
parameter | describe |
---|---|
string | Required. Specifies the string to decode. |
flags | Optional. Specifies how quotation marks are handled and which document type is used. Available quote types: ENT_COMPAT - Default. Only double quotes are decoded. ENT_QUOTES - Decode double and single quotes. ENT_NOQUOTES - Do not decode any quotes. Additional flags specifying the document type to use: ENT_HTML401 - Default. Code processed as HTML 4.01. ENT_HTML5 - code processed as HTML 5. ENT_XML1 - Code processed as XML 1. ENT_XHTML - as XHTML processing code. |
Return value: | Returns the converted string. |
---|---|
PHP version: | 5.1.0+ |
Update log: | In PHP 5.4, additional flags have been added to specify the document type used: ENT_HTML401, ENT_HTML5, ENT_XML1, and ENT_XHTML. |
Convert some predefined HTML entities to characters:
<?php $str = " Jane & 'Tarzan' " ; echo htmlspecialchars_decode ( $str , ENT_COMPAT ) ; // Default, only decode double quotes echo " <br> " ; echo htmlspecialchars_decode ( $str , ENT_QUOTES ) ; // Decode double quotes and single quotes echo " <br> " ; echo htmlspecialchars_decode ( $str , ENT_NOQUOTES ) ; // Do not decode any quotes ?>The HTML output of the above code is as follows (view source code):
< ! DOCTYPE html > <html> < body > Jane & 'Tarzan' < br > Jane & ' Tarzan ' < br > Jane & 'Tarzan' </ body > </html>The browser output of the above code is as follows:
Jane & 'Tarzan'Jane & 'Tarzan'Jane & 'Tarzan'Convert predefined HTML entities to double quotes:
<?php $str = ' I love "PHP". ' ; echo htmlspecialchars_decode ( $str , ENT_QUOTES ) ; // Decode double quotes and single quotes ?>The HTML output of the above code is as follows (view source code):
< ! DOCTYPE html > <html> < body > I love "PHP". </ body > </html>The browser output of the above code is as follows:
I love "PHP".