$text = "\xd0\xa2\xd0\xb0\xd0\xb9\xd0\xbd\xd0\xb0";
$text = iconv('UTF-8', 'UTF-8//IGNORE', $text);
var_dump($text); //Тайна - good
$text = file_get_contents('log.txt');
$text = iconv('UTF-8', 'UTF-8//IGNORE', trim($text));
var_dump($text); // \xd0\xa2\xd0\xb0\xd0\xb9\xd0\xbd\xd0\xb0 - bad
Why if string \xd0\xa2\xd0\xb0\xd0\xb9\xd0\xbd\xd0\xb0
was read from file iconv
did not work and how to fix it ?
The string literal and the text in the file is not equivalent.
$text
is already utf-8 (Тайна) and iconv does nothing to it. This is because you use escape sequences to put the actual binary value in the string. with the data in the file\xd0\xa2\xd0\xb0\xd0\xb9\xd0\xbd\xd0\xb0
is not escaped because it was read from a file and stored in a variable so its not a string literal. Try this to convert the data