mraza Posted April 14, 2011 Share Posted April 14, 2011 Hi, I have few files which i need to display in correct format and also save in db to use. My problem is here is my code: $file = "C:\\file.txt"; $data = file_get_contents($file); $data = '<pre>'; $data .= mb_convert_encoding($file, 'UTF-8', 'ISO-8859-1'); $data .= '</pre>'; echo $data; but when i echo $data i get weird characters . here is result http://paste2.org/p/1361878 Thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/233654-file-encoding-problem/ Share on other sites More sharing options...
requinix Posted April 14, 2011 Share Posted April 14, 2011 You didn't change the encoding of the file data - just the filename. $file = "C:\\file.txt"; $data = file_get_contents($file); $data = mb_convert_encoding($data, "UTF-8", "ISO-8859-1"); // header("Content-Type: text/plain; charset=utf-8"); echo $data; Quote Link to comment https://forums.phpfreaks.com/topic/233654-file-encoding-problem/#findComment-1201357 Share on other sites More sharing options...
php-lover Posted April 14, 2011 Share Posted April 14, 2011 try this $data = htmlentities($data, ENT_QUOTES, "UTF-8"); echo $data; Quote Link to comment https://forums.phpfreaks.com/topic/233654-file-encoding-problem/#findComment-1201369 Share on other sites More sharing options...
mraza Posted April 14, 2011 Author Share Posted April 14, 2011 thanks for replies. I tried both way but did not worked. here is original file content http://www.pastecode.info/view/29305328 and here is problem if i use above codes http://www.pastecode.info/view/75500574 when i tried with htmlentities($data, ENT_QUOTES, "UTF-8"); i get cahrs like this and i had to use $data = mb_convert_encoding($data, "UTF-8", "ISO-8859-1"); or it just display blank page. ßßßÿÿÿ my code again: $file = "C:\file.txt"; $data = file_get_contents($file); // tried three of them one by one and together etc but same problem. //$data = mb_convert_encoding($data, "UTF-8", "ISO-8859-1"); //$data = htmlentities($data, ENT_QUOTES, "UTF-8"); //header("Content-Type: text/plain; UTF-8"); echo $data; thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/233654-file-encoding-problem/#findComment-1201578 Share on other sites More sharing options...
dcro2 Posted April 14, 2011 Share Posted April 14, 2011 header("Content-Type: text/plain; charset=UTF-8"); Change text/plain to text/html if you're going to be using it inside html code. Quote Link to comment https://forums.phpfreaks.com/topic/233654-file-encoding-problem/#findComment-1201581 Share on other sites More sharing options...
mraza Posted April 14, 2011 Author Share Posted April 14, 2011 Thanks but when i use above i get question marks ���������� my code $file = "C:\file.txt"; $data = file_get_contents($file); // tried with this too // $data = mb_convert_encoding($data, "UTF-8", "ISO-8859-1"); header("Content-Type: text/html; charset=UTF-8"); echo $data; also when i did detect i get UTF-8 Result $data = file_get_contents($file); echo mb_detect_encoding($data); // echoed UTF-8 Quote Link to comment https://forums.phpfreaks.com/topic/233654-file-encoding-problem/#findComment-1201583 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.