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
htmlspecialchars_decode
(PHP 5 >= 5.1.0)
htmlspecialchars_decode — 特殊な HTML エンティティを文字に戻す
説明
string htmlspecialchars_decode
( string $string
[, int $quote_style
] )
この関数は htmlspecialchars() の反対です。 特殊な HTML エンティティを文字に戻します。
変換されるエンティティは次のものです。&、 " (ENT_NOQUOTES が設定されていない場合)、 ' (ENT_QUOTES が設定されている場合)、 < および >。
パラメータ
- string
-
デコードする文字列。
- quote_style
-
クォートの形式。以下の定数のいずれかです。
quote_style 定数 定数名 説明 ENT_COMPAT ダブルクォートを変換し、シングルクォートはそのままに します(デフォルト)。 ENT_QUOTES ダブルクォート、シングルクォートの両方を変換します。 ENT_NOQUOTES ダブルクォート、シングルクォートの両方をそのままにします。
返り値
デコードされた文字列を返します。
例
例1 htmlspecialchars_decode() の例
<?php
$str = '<p>this -> "</p>';
echo htmlspecialchars_decode($str);
// クォートが変換されないことに注意しましょう
echo htmlspecialchars_decode($str, ENT_NOQUOTES);
?>
上の例の出力は以下となります。
<p>this -> "</p> <p>this -> "</p>
htmlspecialchars_decode
thomas at xci[ignore_this]teit dot commm
28-Mar-2008 08:03
28-Mar-2008 08:03
Wout
29-Jul-2007 01:06
29-Jul-2007 01:06
The following replacement for PHP 4 is a little more complete, as the quote_style is taken into account as well:
if (!function_exists("htmlspecialchars_decode")) {
function htmlspecialchars_decode($string, $quote_style = ENT_COMPAT) {
return strtr($string, array_flip(get_html_translation_table(HTML_SPECIALCHARS, $quote_style)));
}
}
17-Aug-2006 09:49
This should be the best way to do it.
(Reposted because the other one seems a bit slower and because those who used the code under called it htmlspecialchars_decode_php4)
<?php
if ( !function_exists('htmlspecialchars_decode') )
{
function htmlspecialchars_decode($text)
{
return strtr($text, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
}
}
?>
TheSin
10-May-2006 06:51
10-May-2006 06:51
Here is how you can get this function in php < 5.1, just make sure this function is before you try and call the function.
if (!function_exists('htmlspecialchars_decode')) {
function htmlspecialchars_decode($str, $options="") {
$trans = get_html_translation_table(HTML_SPECIALCHARS, $options);
$decode = ARRAY();
foreach ($trans AS $char=>$entity) {
$decode[$entity] = $char;
}
$str = strtr($str, $decode);
return $str;
}
}
se at designlinks dot net
15-Dec-2005 01:43
15-Dec-2005 01:43
The code supplied by or-k at or-k dot com (14-Sep-2005 09:15) is better served using html_entity_decode() for PHP>=4.3.0.
geoffers@gmail (14-Jul-2005 01:38) offers the best htmlspecialchars_decode() for php4 users.
or-k at or-k dot com
14-Sep-2005 06:15
14-Sep-2005 06:15
that works also with ä and " and so on.
get_html_translation_table(HTML_ENTITIES) => offers more characters than HTML_SPECIALCHARS
function htmlspecialchars_decode_PHP4($uSTR)
{
return strtr($uSTR, array_flip(get_html_translation_table(HTML_ENTITIES, ENT_QUOTES)));
}
geoffers@gmail
14-Jul-2005 10:38
14-Jul-2005 10:38
[Update of previous note, having noticed I forgot to put in quote style]
PHP4 Compatible function:
<?php
function htmlspecialchars_decode_php4 ($str, $quote_style = ENT_COMPAT) {
return strtr($str, array_flip(get_html_translation_table(HTML_SPECIALCHARS, $quote_style)));
}
?>
geoffers at gmail dot com
14-Jul-2005 10:30
14-Jul-2005 10:30
For PHP4 Compatibility:
<?php
function htmlspecialchars_decode_php4 ($str) {
return strtr($str, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
}
?>
