Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

htmlentities

Converts all applicable characters to HTML entities.

Syntax

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

Parameters

ParameterTypeDescription
$stringstringThe input string.
$flagsintOptional. How to treat quotes.
$encoding?stringOptional. The encoding, UTF-8 by default.

Return value

stringThe string with entities.

Examples

Compared with htmlspecialchars():

<?php
 
echo htmlentities('Café <b>€5</b>'), "\n";
echo htmlspecialchars('Café <b>€5</b>');

Output:

Caf&eacute; &lt;b&gt;&euro;5&lt;/b&gt;
Café &lt;b&gt;€5&lt;/b&gt;

Notes

For output on a UTF-8 page htmlspecialchars() is enough: it replaces only the dangerous characters.

See also

In the tutorial

  • Security — The basics of PHP site security — XSS and escaping, SQL injection, password hashing, CSRF tokens and validating input.

Thanks for reading!

Donate to the site — we work hard for you.