(PHP 5 >= 5.1.0, PHP 7, PHP 8)
htmlspecialchars_decode — 特殊な HTML エンティティを文字に戻す
$string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401): string この関数は htmlspecialchars() の反対です。 特殊な HTML エンティティを文字に戻します。
変換されるエンティティは次のものです。&、
" (ENT_NOQUOTES が設定されていない場合)、
' (ENT_QUOTES が設定されている場合)、
< および >。
stringデコードする文字列。
flags
以下のフラグのビットマスクによる組み合わせで、クォートの扱いやドキュメントの形式を指定します。
デフォルトは ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 です。
| 定数名 | 説明 |
|---|---|
ENT_COMPAT |
ダブルクォートを変換し、シングルクォートはそのままにします。 |
ENT_QUOTES |
ダブルクォート、シングルクォートの両方を変換します。 |
ENT_NOQUOTES |
ダブルクォート、シングルクォートの両方をそのままにします。 |
ENT_SUBSTITUTE |
無効な符号単位シーケンスを含む文字列を渡したときに、 空の文字列を返すのではなく Unicode の置換文字に置き換えます。 UTF-8 の場合は U+FFFD、それ以外の場合は &#FFFD; となります。 |
ENT_HTML401 |
コードを HTML 4.01 として処理します。 |
ENT_XML1 |
コードを XML 1 として処理します。 |
ENT_XHTML |
コードを XHTML として処理します。 |
ENT_HTML5 |
コードを HTML 5 として処理します。 |
デコードされた文字列を返します。
| バージョン | 説明 |
|---|---|
| 8.1.0 |
flags のデフォルト値が
ENT_COMPAT から
ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401
に変更されました。
|
例1 htmlspecialchars_decode() の例
<?php
$str = "<p>this -> "</p>\n";
echo htmlspecialchars_decode($str);
// クォートが変換されないことに注意しましょう
echo htmlspecialchars_decode($str, ENT_NOQUOTES);
?>上の例の出力は以下となります。
<p>this -> "</p> <p>this -> "</p>
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, ThomasThis 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)));
}
}
?>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)));
}