ICU has functions to detect charset encodings, here and PHP also has ICU extension, here but how to use php icu extension to do the charset detection? I can not find anything related to this part on php's doc
It may be a new feature request for the PHP extension, or an opportunity to contribute it yourself.