note 82133 added to function.htmlspecialchars-decode
| From: | thomasatxci | Date: | Fri, 28 Mar 2008 11:03:31 +0000 |
| Subject: | note 82133 added to function.htmlspecialchars-decode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-137668@lists.php.net to get a copy of this message | ||
The example for "htmlspecialchars_decode()" below sadly does not work for all PHP4
versions.
Quote from the PHP manual:
"get_html_translation_table() will return the translation table that is used internally for
htmlspecialchars() and htmlentities()."
But it does NOT! At least not for PHP version 4.4.2.
This was already reported in a bug report (http://bugs.php.net/bug.php?id=25927), but it was marked
as BOGUS.
Proof:
Code:
--------------------
<?php
var_dump(get_html_translation_table(HTML_SPECIALCHARS,ENT_QUOTES));
var_dump(htmlspecialchars('\'',ENT_QUOTES));
?>
--------------------
Output:
--------------------
array
'"' => '"'
''' => '''
'<' => '<'
'>' => '>'
'&' => '&'
'''
--------------------
This comment now is not to report this bug again (though I really believe it is one), but to
complete the example and warn people of this pitfall.
To make sure your htmlspecialchars_decode fake for PHP4 works, you should do something like this:
<?php
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);
}
?>
Br, Thomas
----
Server IP: 217.13.201.10
Probable Submitter: 81.223.132.98
----
Manual Page -- http://www.php.net/manual/en/function.htmlspecialchars-decode.php
Edit -- https://master.php.net/note/edit/82133
Del: integrated -- https://master.php.net/note/delete/82133/integrated
Del: useless -- https://master.php.net/note/delete/82133/useless
Del: bad code -- https://master.php.net/note/delete/82133/bad+code
Del: spam -- https://master.php.net/note/delete/82133/spam
Del: non-english -- https://master.php.net/note/delete/82133/non-english
Del: in docs -- https://master.php.net/note/delete/82133/in+docs
Del: other reasons-- https://master.php.net/note/delete/82133
Reject -- https://master.php.net/note/reject/82133
Search -- https://master.php.net/manage/user-notes.php