Why does the PHP function htmlentities(…) returns wrong results?

青春壹個敷衍的年華 提交于 2019-12-22 08:19:13

问题


I have the following code :

function testAccents() {
    $str = "àéè";
    $html = htmlentities($str);
    echo $html;
}

When I run it, instead of getting àéè I get àéè.

I thought that it could be a problem of encoding but the file is utf-8 :

> file -bi PublicationTest.php 
  text/x-c++; charset=utf-8

Why do I get this strange result ?

EDIT: I use PHP 5.3.


回答1:


Before PHP 5.4.0, htmlentities() expects ISO-8859-1 data by default. It's interpreting your UTF-8 input as single-byte characters, which results in the funny results you get.

Specify the encoding specifically.

$html = htmlentities($str, ENT_COMPAT, "UTF-8");


来源:https://stackoverflow.com/questions/13246903/why-does-the-php-function-htmlentities-returns-wrong-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!