PHP htmlentities() 没有按预期工作

我在使用 htmlentities() 时遇到问题

I'm having a problem with htmlentities()

$txt = "árbol";
echo $txt; // outputs: árbol
echo htmlentities($txt); // outputs: árbol (árbol)

第二个回声应该输出 árbol (á)

我使用的是 utf-8:

I'm using utf-8:

<meta charset="utf-8">

这是怎么回事?谢谢!

推荐答案

你必须设置 htmlentities() 的第三个参数,它告诉字符集使用.因为你没有设置,所以使用默认值,默认是ISO-8859-1,不是UTF-8.

You have to set the third parameter of htmlentities() which tells the charset to use. Because of you don't set it, the default is used and the default is ISO-8859-1, not UTF-8.

与 htmlspecialchars() 一样,它采用可选的第三个参数 charset,它定义了转换中使用的字符集.目前,默认使用 ISO-8859-1 字符集.

Like htmlspecialchars(), it takes an optional third argument charset which defines character set used in conversion. Presently, the ISO-8859-1 character set is used as the default.

澄清一下,这是函数签名:

Just to clarify, this is the function signature:

string htmlentities ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $charset [, bool $double_encode = true ]]] )

在这里你会找到官方文档:http://php.net/手册/en/function.htmlentities.php

and here you'll find the official doc: http://php.net/manual/en/function.htmlentities.php

相关文章