Stuck UTF-8 file via PHP fwrite

I cannot understand what I am doing wrong. I get the contents of a file from the database. When I echo the content, everything is displayed very well, when I write it to a file (.html), it breaks. I tried iconv and several other solutions, but I just don’t understand what I have to set for the first parameter, I tried spaces, and that is also not very good. I assume that it exits the database as UTF-8 if it echoes correctly. Stuck a bit now with no luck.

function file($fileName, $content) {
    if (!file_exists("out/".$fileName)) {
        $file_handle = fopen(DOCROOT . "out/".$fileName, "wb") or die("can't open file");
        fwrite($file_handle, iconv('UTF-8', 'UTF-8', $content));
        fclose($file_handle);
        return TRUE;
    } else {
        return FALSE;
    }
}

The source of the html file is as follows.

Exits the database as follows:

<h5>   CMS</h5>

goes in a file like this

<h5>Ð¢ÐµÐºÑƒÑ‰Ð°Ñ ÑÑ‚Ð°Ð±Ð¸Ð»ÑŒÐ½Ð°Ñ Ð²ÐµÑ€ÑÐ¸Ñ CMS</h5>

EDIT:

It turns out that the root of the problem was Apache serving files incorrectly. Adding

AddDefaultCharset utf-8

In my .htaccess file fixed it. The clock is gone ... At least I found out something.

+4
2

: , , ,

, UTF-8

, , , ? , , .

, , - MySQL MySQL // UTF8_general_ci?

- MySQL UTF8 UTF8, 3 , 4 , UTF-8, . UTF -8 .

, MySQL UTF8_ UTF8mb4_ ( : MySQL 5.5.3), UTF8_multibyte_4, UTF-8.

, - PHP , mb_ PHP .

, , , , UTF8mb4, , , , 3- UTF8, PHP 4- UTF8 .

, , MySQL, , !

:

function file($fileName, $content) {
    if (!file_exists("out/".$fileName)) {
        $file_handle = fopen(DOCROOT . "out/".$fileName, "wb") or die("can't open file");
        fwrite($file_handle, iconv('UTF-8', 'UTF-8', $content));
        fclose($file_handle);
        return TRUE;
    } else {
        return FALSE;
    }
}
  • $file_handle if, , .

  • iconv , "utf-8" er, "utf-8". , , /, , , , .

+1

. HTML <meta charset="UTF-8"> <head>.

iconv , , UTF-8, .

. UTF-8, .

!

0

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


All Articles