I have a script that reads data from text file encoded in utf8. It reads characters one by one with fgetc(). When it reads simple ascii charactes it's fine but when it comes to š, č, ž... It doesn't work correctly. The simplified code looks like this:
$file = fopen($path);
$char = fgetc($file);
while( $char !== false) {
$char = fgetc($file);
fwrite(STDOUT, $char);
}
I tried to use
header('Content-type: text/plain; charset=utf-8');
at the beginning of the script, but it still doesn't work. I also tried to use utf8_encode($char) or utf8_decode($char), it didn't help. Is there any simple solution how to read utf8 characters and write them to output?
UPDATE:
The problem is that special characters are saved in two indexes, so when I call one fgetc I don't get the whole character. My solution for now is that when I get a character with ordinal number above 127 I call fgetc again and make a string from those two values from fgetc, then I can correctly fwrite loaded special character. Maybe it's not the best solution but I couldn't figure out anything better.