{"id":3200,"date":"2018-08-19T10:05:43","date_gmt":"2018-08-19T10:05:43","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=3200"},"modified":"2018-08-19T10:05:43","modified_gmt":"2018-08-19T10:05:43","slug":"php-file_get_contents-for-utf-encoded-content","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2018\/08\/19\/php-file_get_contents-for-utf-encoded-content\/","title":{"rendered":"PHP file_get_contents for UTF encoded content"},"content":{"rendered":"            <script type=\"text\/javascript\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushPhp.js\"><\/script>\n<p>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.<\/p>\n<p>The solution is to\u00a0 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.\u00a0 For Arabic it would be ISO-8859-6 and so on.<\/p>\n<p>Sample code given below as a function:<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><pre class=\"brush: php\">function file_get_contents_utf8($fn) {\r\n     $content = file_get_contents($fn);\r\n      return mb_convert_encoding($content, &#039;UTF-8&#039;,\r\n          mb_detect_encoding($content, &#039;UTF-8, ISO-8859-1&#039;, true));\r\n}<\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>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. <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2018\/08\/19\/php-file_get_contents-for-utf-encoded-content\/\" title=\"PHP file_get_contents for UTF encoded content\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2357,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-3200","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apachephp"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=3200"}],"version-history":[{"count":2,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3200\/revisions"}],"predecessor-version":[{"id":3202,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/3200\/revisions\/3202"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2357"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=3200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=3200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=3200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}