I needed to use the PHP function htmlspecialchars_decode( ) for a WordPress widget I am making. This function is built into PHP versions 5.1.0 and greater and is used to convert special HTML entities to characters. As defined, htmlspecialchars_decode( ) is the opposite of htmlspecialchars( ). Someone named Thomas commented on the PHP man page to point out a flaw in the definition. He also provides some code, which I have only modified slightly below to check function_exists( ).
if ( !function_exists('htmlspecialchars_decode') ){
function htmlspecialchars_decode($string,$style=ENT_COMPAT)
{
$translation = array_flip(get_html_translation_table(HTML_SPECIALCHARS,$style));
if($style === ENT_QUOTES){ $translation['''] = '\''; }
return strtr($string,$translation);
}
}
Comments
2 responses to “PHP4 Friendly htmlspecialchars_decode”
This is not working for ‘ and ’ ?
Ken:
I am not sure what you mean. The purpose of this function is to handle special characters in URLs, not plain text.