为什么PHP函数htmlentities(...)返回错误的结果? [英] Why does the PHP function htmlentities(...) returns wrong results?

查看:62
本文介绍了为什么PHP函数htmlentities(...)返回错误的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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

运行它时,我得到的不是àéè,而是得到的àéè.

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

我认为这可能是编码问题,但是文件是utf-8:

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 ?

我使用PHP 5.3.

I use PHP 5.3.

推荐答案

在PHP 5.4.0之前, htmlentities() 要求ISO-8859默认为-1数据.它将您的UTF-8输入解释为单字节字符,这将导致您得到有趣的结果.

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.

具体指定编码.

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

这篇关于为什么PHP函数htmlentities(...)返回错误的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆