0

try this code!

<?php
$handle = fopen("./test.txt", "r");
$serachEncoding = 'CP1251';
while (!feof($handle)) {
    $buffer = fgets($handle, 4096);
    $buffer = iconv("UTF8", "CP1251//TRANSLIT//IGNORE", $buffer);
    $charset = mb_detect_encoding($buffer, [$serachEncoding]);
    echo $charset;
}
fclose($handle);

if $serachEncoding = UTF8 the file will be read line by line and will output its encoding on each line

if $serachEncoding = CP1251 will output one line and its encoding

So why?

  • 1
    Do you mean it reads the whole file as one line, or that it just outputs the first actual line and then nothing more afterwards? If the latter, do you have proper PHP error reporting enabled? – 04FS Oct 25 '19 at 06:40
  • "UTF8" is an illegal encoding in `$buffer = iconv("UTF8", "CP1251//TRANSLIT//IGNORE", $buffer);` it is not supported. It should be `$buffer = iconv("UTF-8", "CP1251//TRANSLIT//IGNORE", $buffer);` at least. Turn on proper error reporting - you will get a `Notice: iconv(): Wrong charset..` Not even a warning. Which is sad. – Eugene Anisiutkin Oct 25 '19 at 07:12
  • @04FS Just displays Windows-1251 I'm check my error reporting – Вячеслав Прокопченко Oct 25 '19 at 09:51

1 Answers1

0

Okay, i'm a debil i add var_dump my variable with charset and dispaly now

bool(false)
bool(false)
bool(false)
string(12) "Windows-1251"