Learning the Character Encoding of a File with PHP

It stumbled upon me recently when I was loading subtitles. Some subtitles are UTF-8 and some are ISO-8859-9. That's why the codes I wrote were showing one properly but not the other. So I needed to find out what character encoding the file belonged to. And I wrote a code like this;

$open = file_get_contents('test.srt');
$enc = mb_detect_encoding($open, mb_list_encodings(), true);

echo $enc; // result: UTF-8 ya da ISO-8859-9

So what did it do for me to find this? In this way, I was able to determine the character set with the header as follows;

if ($enc == "UTF-8") {
    header('Content-type: text/vtt; charset=utf8');
} else {
    header('Content-type: text/vtt; charset=iso-8859-9');

This solved my character problems. If you encounter such an error one day, you now know the solution, take it easy.


There are no comments, make the firs comment