It’s not just with the <
, >
and &
, htmlentities
is much more than that
htmlspecialchars
Description
string htmlspecialchars ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") [, bool $double_encode = true ]]] )
It will convert characters to entities that affect HTML, with the following conversions:
&
will become &
>
will become <
<
will become >
"
will become "
(except when ENT_NOQUOTES
is defined in $flags
)
'
will become &
, converts to '
when $flags
has ENT_HTML401
or '
when ENT_XML1
, ENT_XHTML
or ENT_HTML5
, but only when defined $flags
with ENT_QUOTES
htmlentities
Description
string htmlentities ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") [, bool $double_encode = true ]]] )
The behavior is identical to htmlspecialchars
by default to &
, >
, <
, "
and '
specifically, that is in no way changes (for this reason I disagree with the other reply), what htmlentities
differs is that in addition to the cited characters, it will convert all characters that have representation in HTML entities, follows lists of characters (probably complete):
A simple example of difference are accents:
<?php
echo htmlspecialchars('<foo><bar>Olá Mundo!</bar></foo>'), "\n";
echo htmlentities('<foo><bar>Olá Mundo!</bar></foo>'), "\n";
The result will be this:
<foo><bar>Olá Mundo!</bar></foo>
<foo><bar>Olá Mundo!</bar></foo>
Example in IDEONE
Note also that the behavior of both functions can be adjusted by flags:
ENT_COMPAT
, ENT_QUOTES
, ENT_NOQUOTES
, ENT_SUBSTITUTE
, ENT_DISALLOWED
, ENT_HTML401
, ENT_XML1
, ENT_XHTML
and ENT_HTML5
In other words, this reinforces that what differs in both functions are not the characters < > &
quoted in the other reply:
They do the same thing except for a few characters " < > & "
Behaviours
Other behaviors may vary according to these $flags
which I have already quoted, and can also change with the use of the $encoding
and $double_encode
, however these are specific settings as required.
See related English: http://stackoverflow.com/questions/46483/htmlentities-vs-htmlspecialchars
– Wallace Maxters
What would be an html reality? an html tag only dimension?
– rray
Tbm didn’t quite understand the definition :(
– DiChrist
@Dichrist even read mine reply?
– Guilherme Nascimento
I just read. In this case she answers my question more satisfactorily.
– DiChrist