PHP html_entity_decode()関数
例
HTMLエンティティを文字に変換します。
<?php
$str = '<a href="https://www.w3schools.com">w3schools.com</a>';
echo html_entity_decode($str);
?>
上記のコードのHTML出力は(ソースの表示)になります。
<a href="https://www.w3schools.com">w3schools.com</a>
上記のコードのブラウザ出力は次のようになります。
定義と使用法
html_entity_decode()関数は、HTMLエンティティを文字に変換します。
html_entity_decode()関数は、 htmlentities()の反対です。
構文
html_entity_decode(string,flags,character-set)
パラメータ値
Parameter | Description |
---|---|
string | Required. Specifies the string to decode |
flags | Optional. Specifies how to handle quotes and which document type to use. The available quote styles are:
Additional flags for specifying the used doctype:
|
character-set | Optional. A string that specifies which character-set to use. Allowed values are:
Note: Unrecognized character-sets will be ignored and replaced by ISO-8859-1 in versions prior to PHP 5.4. As of PHP 5.4, it will be ignored an replaced by UTF-8. |
技術的な詳細
戻り値: | 変換された文字列を返します |
---|---|
PHPバージョン: | 4.3.0+ |
変更ログ: | PHP 5.6-文字セット
パラメータのデフォルト値を(構成内の)デフォルトの文字セットの値に変更しました。 PHP5.4-文字セットパラメータのデフォルト値をUTF-8に変更しました。 PHP 5.4-ENT_HTML401、ENT_HTML5、ENT_XML1、およびENT_XHTMLを追加しました。 PHP5.0-マルチバイトエンコーディングのサポートが追加されました |
その他の例
例
一部のHTMLエンティティを文字に変換します。
<?php
$str = "Albert Einstein said: 'E=MC²'";
echo
html_entity_decode($str, ENT_COMPAT); // Will only convert double quotes
echo "<br>";
echo html_entity_decode($str, ENT_QUOTES); // Converts double
and single quotes
echo "<br>";
echo html_entity_decode($str, ENT_NOQUOTES); // Does not
convert any quotes
?>
上記のコードのHTML出力は(ソースの表示)になります。
Albert Einstein said: 'E=MC²'<br>
Albert Einstein said: 'E=MC²'<br>
Albert Einstein said: 'E=MC²'
上記のコードのブラウザ出力は次のようになります。
Albert Einstein said: 'E=MC²'
Albert Einstein said: 'E=MC²'
Albert
Einstein said: 'E=MC²'
例
西ヨーロッパの文字セットを使用して、一部のHTMLエンティティを文字に変換します。
<?php
$str = "My name is Øyvind Åsane. I'm Norwegian.";
echo html_entity_decode($str, ENT_QUOTES, "UTF-8");
?>
上記のコードのHTML出力は(ソースの表示)になります。
My name is Øyvind Åsane. I'm Norwegian.
上記のコードのブラウザ出力は次のようになります。
My name is Øyvind Åsane. I'm Norwegian.
❮PHP文字列リファレンス