Php 为什么htmlentities功能不能正常工作?

Php 为什么htmlentities功能不能正常工作?,php,html-entities,Php,Html Entities,我用PHP 5尝试了htmlentities()函数,代码如下: <?php $string="Einstürzende Neubauten"; echo htmlentities($string); ?> 并且它只显示两个空格(即“”)。为什么呢?我试着用另一个字符替换“u with diaeresis”字符,结果成功了。我如何也能完成这项工作?对给定内容使用字符集以。。。。乙二醇 $res = htmlentities ( $string, ENT_COMPAT, 'U

我用PHP 5尝试了
htmlentities()
函数,代码如下:

<?php
 $string="Einstürzende Neubauten"; echo htmlentities($string);
?>


并且它只显示两个空格(即“”)。为什么呢?我试着用另一个字符替换“u with diaeresis”字符,结果成功了。我如何也能完成这项工作?

对给定内容使用字符集以。。。。乙二醇

 $res = htmlentities ( $string, ENT_COMPAT, 'UTF-8');
有关更多信息,请查看

您使用了哪个PHP版本

也许这对你来说是个解决办法

 $string = mb_convert_encoding ($str , "UTF-8");
 // testing 
    var_dump($string);
 $res = htmlentities ( $string, ENT_COMPAT, 'UTF-8');
 // testing
    var_dump($res);

请参见

当我将PHP版本从5.2升级到5.6时,我遇到了同样的问题。我写道:

$res = htmlentities("Producción", ENT_IGNORE);
我得到了

Produccin
但我解决了这个问题,在连接到数据库后添加了这个

mysqli_set_charset($idCon,'utf8'); 

右键单击并查看页面查看源代码。是的,对我来说很有效可能是编码问题。您是否尝试过
meta charset=utf-8
?我尝试过使用utf-8,但源代码不包含字符串。无论如何,谢谢。它是用ANSI编码的.php文件(默认情况下在Windows中使用记事本)。我改变了它,在php和apache配置中设置了UTF-8编码,现在一切正常。