Jump to content

Change encoding of a file


hno

Recommended Posts

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.