PHP 8.4.0 RC3 available for testing

html_entity_decode

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

html_entity_decodeHTML エンティティを対応する文字に変換する

説明

html_entity_decode(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null): string

html_entity_decode()htmlentities() の反対で、string にある HTML エンティティを対応する文字に変換します。

厳密に言うと、この関数は次の二つの条件を満たすすべての (数値エンティティを含む) エンティティをデコードします。それ以外のエンティティは、何も変換しません。 1) 選択したドキュメントタイプで必然的に有効になるもの。つまり XML の場合には、DTD で定義されている名前付きエンティティはデコードしません。 2) 選択したエンコーディングに関連づけられている符号化文字集合に含まれる文字で、 選択したドキュメントタイプで許可されているもの。

パラメータ

string

入力文字列。

flags

以下のフラグのビットマスクによる組み合わせで、クォートの扱いやドキュメントの形式を指定します。 デフォルトは ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 です。

使用可能な flags 定数
定数名 説明
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 として処理します。

encoding

オプションの引数。文字を変換するときに使うエンコーディングを定義します。

省略した場合の encoding のデフォルト値は、 default_charset の値を使います。

技術的にはこの引数を省略可能ですが、 default_charset の指定が入力とは違う文字セットになっている可能性もあるので、 適切な値を指定しておくことを強く推奨します。

以下の文字セットをサポートします。

サポートする文字セット
文字セット エイリアス 説明
ISO-8859-1 ISO8859-1 西欧、Latin-1
ISO-8859-5 ISO8859-5 ほとんど使われないキリル文字セット (Latin/Cyrillic)。
ISO-8859-15 ISO8859-15 西欧、Latin-9 。Latin-1(ISO-8859-1) に欠けている ユーロ記号やフランス・フィンランドの文字を追加したもの。
UTF-8   ASCII 互換のマルチバイト 8 ビット Unicode 。
cp866 ibm866, 866 DOS 固有のキリル文字セット。
cp1251 Windows-1251, win-1251, 1251 Windows 固有のキリル文字セット。
cp1252 Windows-1252, 1252 西欧のための Windows 固有の文字セット。
KOI8-R koi8-ru, koi8r ロシア語。
BIG5 950 繁体字中国語。主に台湾で使用されます。
GB2312 936 簡体字中国語。国の標準文字セットです。
BIG5-HKSCS   Big5 に香港の拡張を含めたもの。繁体字中国語。
Shift_JIS SJIS, SJIS-win, cp932, 932 日本語。
EUC-JP EUCJP, eucJP-win 日本語。
MacRoman   Mac OS で使われる文字セット。
''   空文字列を指定すると、 スクリプトのエンコーディング (Zend multibyte)、 default_charset、 そして現在のロケール (nl_langinfo() および setlocale() を参照ください) の順でエンコーディングを検出します。 この方法はおすすめしません。

注意: これら以外の文字セットは理解できません。 かわりにデフォルトのエンコーディングを使用し、警告を発生させます。

戻り値

デコードされた文字列を返します。

変更履歴

バージョン 説明
8.1.0 flags のデフォルト値が ENT_COMPAT から ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 に変更されました。
8.0.0 encoding は、nullable になりました。

例1 HTML エンティティのデコード

<?php
$orig
= "I'll \"walk\" the <b>dog</b> now";

$a = htmlentities($orig);

$b = html_entity_decode($a);

echo
$a; // I'll &quot;walk&quot; the &lt;b&gt;dog&lt;/b&gt; now

echo $b; // I'll "walk" the <b>dog</b> now
?>

注意

注意:

trim(html_entity_decode('&nbsp;')); の結果が空の文字列に ならないことを疑問に思う人もいるでしょう。なぜそうなるのかというと、 デフォルトのエンコーディング ISO-8859-1 では '&nbsp;' エンティティが ASCII コード 32 (これは trim() で取り除かれる) ではなく ASCII コード 160 (0xa0) に変換されるからです。

参考

add a note

User Contributed Notes 5 notes

up
130
Martin
13 years ago
If you need something that converts &#[0-9]+ entities to UTF-8, this is simple and works:

<?php
/* Entity crap. /
$input = "Fovi&#269;";

$output = preg_replace_callback("/(&#[0-9]+;)/", function($m) { return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES"); }, $input);

/* Plain UTF-8. */
echo $output;
?>
up
28
txnull
9 years ago
Use the following to decode all entities:
<?php html_entity_decode($string, ENT_QUOTES | ENT_XML1, 'UTF-8') ?>

I've checked these special entities:
- double quotes (&#34;)
- single quotes (&#39; and &apos;)
- non printable chars (e.g. &#13;)
With other $flags some or all won't be decoded.

It seems that ENT_XML1 and ENT_XHTML are identical when decoding.
up
6
aidan at php dot net
20 years ago
This functionality is now implemented in the PEAR package PHP_Compat.

More information about using this function without upgrading your version of PHP can be found on the below link:

http://pear.php.net/package/PHP_Compat
up
-1
Benjamin
11 years ago
The following function decodes named and numeric HTML entities and works on UTF-8. Requires iconv.

function decodeHtmlEnt($str) {
$ret = html_entity_decode($str, ENT_COMPAT, 'UTF-8');
$p2 = -1;
for(;;) {
$p = strpos($ret, '&#', $p2+1);
if ($p === FALSE)
break;
$p2 = strpos($ret, ';', $p);
if ($p2 === FALSE)
break;

if (substr($ret, $p+2, 1) == 'x')
$char = hexdec(substr($ret, $p+3, $p2-$p-3));
else
$char = intval(substr($ret, $p+2, $p2-$p-2));

//echo "$char\n";
$newchar = iconv(
'UCS-4', 'UTF-8',
chr(($char>>24)&0xFF).chr(($char>>16)&0xFF).chr(($char>>8)&0xFF).chr($char&0xFF)
);
//echo "$newchar<$p<$p2<<\n";
$ret = substr_replace($ret, $newchar, $p, 1+$p2-$p);
$p2 = $p + strlen($newchar);
}
return $ret;
}
up
-3
Daniel A.
6 years ago
I wanted to use this function today and I found the documentation, especially about the flags, not particularly helpful.

Running the code below, for example, failed because the flag I used was the wrong one...

$string = 'Donna&#039;s Bakery';
$title = html_entity_decode($string, ENT_HTML401, 'UTF-8');
echo $title;

The correct flag to use in this case is ENT_QUOTES.

My understanding of the flag to use is the one that would correspond to the expected, converted outcome. So, ENT_QUOTES for a character that would be a single or double quote when converted... and so on.

Please help make the documentation a bit clearer.
To Top