関数 htmlentities

関数 htmlentities は文字を 対応するHTMLエンティティに変換します。より理解を深めるために例を参照してください。

この関数は htmlspecialchars と同様ですが、異なり、 HTMLエンティティが存在するすべての文字を変換します。

構文

phphtmlentities(string $string, int $flags = ENT_COMPAT, ?string $encoding = null, bool $double_encode = true): string

例

タグを含む文字列を変換してみましょう:

php<?php
  $str = '<b>text</b>';
  $res = htmlentities($str);
  
  echo $res;
?>

コード実行結果:

php'<b>text</b>'

例

アンパサンドを含む文字列を 変換してみましょう:

php<?php
  $str = 'test: &';
  $res = htmlentities($str);
  
  echo $res;
?>

コード実行結果:

php'test: &'

関連項目