関数 htmlentities
関数 htmlentities は文字を
対応するHTMLエンティティに変換します。より理解を深めるために例を参照してください。
この関数は htmlspecialchars
と同様ですが、異なり、
HTMLエンティティが存在するすべての文字を変換します。
構文
htmlentities(string $string, int $flags = ENT_COMPAT, ?string $encoding = null, bool $double_encode = true): string
例
タグを含む文字列を変換してみましょう:
<?php
$str = '<b>text</b>';
$res = htmlentities($str);
echo $res;
?>
コード実行結果:
'<b>text</b>'
例
アンパサンドを含む文字列を 変換してみましょう:
<?php
$str = 'test: &';
$res = htmlentities($str);
echo $res;
?>
コード実行結果:
'test: &'
関連項目
-
逆の操作を行う関数
html_entity_decode、 -
類似の操作を行う関数
htmlspecialchars、