foreach ($encodings as $enc) // mb_check_encoding is stricter than mb_detect_encoding if (mb_check_encoding($string, $enc)) return $enc;

function get_encoding($str) $list = ['UTF-8', 'ISO-8859-1', 'Windows-1252']; foreach ($list as $item) if (mb_check_encoding($str, $item)) return $item; return false; Use code with caution. Copied to clipboard 3. Detecting Files via BOM

This is faster and more reliable than detection if you have a specific target in mind.

Back to top