Why does the PHP htmlentities (...) function return incorrect 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 this might be an encoding problem, but the utf-8 file is:

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

Why am I getting this strange result?

EDIT: I am using PHP 5.3.

+4
source share
1 answer

Prior to PHP 5.4.0, htmlentities() defaults to ISO-8859-1 data. It interprets your UTF-8 input as single-byte characters, which leads to the ridiculous results you get.

Specify a specific encoding.

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

Source: https://habr.com/ru/post/1444279/


All Articles