PHP file_get_contents for UTF encoded content

One of the problems of file_get_contents() is that it messes up the data if the file contains special characters outside the standard ASCII character set.

The solution is to  convert the encoding of the contents to UTF-8, but only after it has detected the desired encoding. So for instance if we know the file contains European languages like Spanish or French then we specify the detection for ISO-8859-1.  For Arabic it would be ISO-8859-6 and so on.

Sample code given below as a function:

 

 

 

function file_get_contents_utf8($fn) {
     $content = file_get_contents($fn);
      return mb_convert_encoding($content, 'UTF-8',
          mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
}

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.