hno Posted January 17, 2010 Share Posted January 17, 2010 HI I have a file with ANSI encoding and the file type is txt.I want to change the file Encoding to UTF-8 .How can do so? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/188779-change-encoding-of-a-file/ Share on other sites More sharing options...
Buddski Posted January 17, 2010 Share Posted January 17, 2010 Do you want to do this with PHP? You can however change the encoding in Windows Notepad.. Quote Link to comment https://forums.phpfreaks.com/topic/188779-change-encoding-of-a-file/#findComment-996649 Share on other sites More sharing options...
hno Posted January 17, 2010 Author Share Posted January 17, 2010 Do you want to do this with PHP? You can however change the encoding in Windows Notepad.. No I want to do that with php and coding? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/188779-change-encoding-of-a-file/#findComment-996651 Share on other sites More sharing options...
Buddski Posted January 17, 2010 Share Posted January 17, 2010 You can read in the file to a variable, apply utf8_encode, and output the result to a new file or overwrite the existing one.. $file = utf8_encode(file_get_contents('text.txt')); file_put_contents('text_utf8.txt',$file); Might work.. this is un-tested Quote Link to comment https://forums.phpfreaks.com/topic/188779-change-encoding-of-a-file/#findComment-996653 Share on other sites More sharing options...
hno Posted January 18, 2010 Author Share Posted January 18, 2010 You can read in the file to a variable, apply utf8_encode, and output the result to a new file or overwrite the existing one.. $file = utf8_encode(file_get_contents('text.txt')); file_put_contents('text_utf8.txt',$file); Might work.. this is un-tested No it doesn't work.any other idea? By the way,is it possible to set it to utf-8 when making a file? thanks Quote Link to comment https://forums.phpfreaks.com/topic/188779-change-encoding-of-a-file/#findComment-996984 Share on other sites More sharing options...
oni-kun Posted January 18, 2010 Share Posted January 18, 2010 You can read in the file to a variable, apply utf8_encode, and output the result to a new file or overwrite the existing one.. $file = utf8_encode(file_get_contents('text.txt')); file_put_contents('text_utf8.txt',$file); Might work.. this is un-tested No it doesn't work.any other idea? By the way,is it possible to set it to utf-8 when making a file? thanks You're not making any sense. What are you attempting to do? Are you wanting to convert a string and place it in a UTF-8 document? Are you wanting to put Unicode formatted characters into the file in the first place? The BOM (Byte Order Mark) is all Windows notepad will spew out, If it is written in UTF-8 it will stay UTF-8 unless you do not use utf8_encode on the string and write it to the file as so. Quote Link to comment https://forums.phpfreaks.com/topic/188779-change-encoding-of-a-file/#findComment-997006 Share on other sites More sharing options...
hno Posted January 18, 2010 Author Share Posted January 18, 2010 You can read in the file to a variable, apply utf8_encode, and output the result to a new file or overwrite the existing one.. $file = utf8_encode(file_get_contents('text.txt')); file_put_contents('text_utf8.txt',$file); Might work.. this is un-tested No it doesn't work.any other idea? By the way,is it possible to set it to utf-8 when making a file? thanks You're not making any sense. What are you attempting to do? Are you wanting to convert a string and place it in a UTF-8 document? Are you wanting to put Unicode formatted characters into the file in the first place? The BOM (Byte Order Mark) is all Windows notepad will spew out, If it is written in UTF-8 it will stay UTF-8 unless you do not use utf8_encode on the string and write it to the file as so. What I want to do is this:I have a mysql backup string which make a text file and put the queries in it and because I have Farsi characters in it the file should be utf-8 but it's ansi so I want to make it utf-8.What do you suggest?I think I have two ways for that first change the made file to utf-8 and another one is I when we are making the file.I don't know it's possible or not to make utf-8 files. Please help me with that. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/188779-change-encoding-of-a-file/#findComment-997227 Share on other sites More sharing options...
oni-kun Posted January 18, 2010 Share Posted January 18, 2010 You should run some quieries on the table to ensure it is in unicode format mysql_query("SET NAMES 'utf8'"); mysql_query("SET CHARACTER SET 'utf8'"); Or export them as a unicode compatible charset. It should save the file in a unicode document. If you want to create a file that is unicode with the proper BOM, you may want to download a capable editor (such as Notepad++) or as mentioned, use the 'encoding as UTF8' in notepad. To read a table that contains unicode characters, you MUST run utf8_encode on the string taken from the database row/file to be able to display the farsi text. Quote Link to comment https://forums.phpfreaks.com/topic/188779-change-encoding-of-a-file/#findComment-997235 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.