The htmlentities Function

The htmlentities function converts characters into corresponding HTML entities. See examples for better understanding.

The function is similar to htmlspecialchars, however, unlike it, converts all characters for which an HTML entity exists.

Syntax

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

Example

Let's convert a string with tags:

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

Code execution result:

php'<b>text</b>'

Example

Let's convert a string with an ampersand:

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

Code execution result:

php'test: &'

See Also