Pergunta

I have a slight confusion regarding using UTF-8 in the htmlspecialchars() function like this:

htmlspecialchars($string, ENT_QUOTES, 'UTF-8');

What I do not understand is, does using this make htmlspecialchars() create a UTF-8 code or that it just informs htmlspecialchars() the $string is UTF-8 so that it does not mess up. I mean are we specifying? the input or are we specifying the output?

Hope it makes sense. Thank you.

Foi útil?

Solução 2

The third parameter is just to tell htmlspecialchars() not to mess up Unicode strings. You need to tell htmlspecialchars() what the input encoding is. Consequentially, that is also the encoding used in the output.

Remember that PHP does not support Unicode, so it does not care about what charset you are using. That parameter in htmlspecialchars() is only used to tell PHP not to mess up with your bytes.

Outras dicas

The third parameter is for both input and output. If you want to convert between character sets, you'll have to do it externally e.g. using the iconv function.

I think the manual is straight forward about this

Defines encoding used in conversion. If omitted, the default value for this argument is ISO-8859-1 in versions of PHP prior to 5.4.0, and UTF-8 from PHP 5.4.0 onwards.

This is about input and output.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top