Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 787
Barth Avatar asked Dec 06 '22 11:12

Barth


1 Answers

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");
like image 64
Pekka Avatar answered Jan 04 '23 23:01

Pekka