Forum Moderators: coopster

Message Too Old, No Replies

when writing to file, encoding utf-8 > ANSI, want to keep utf-8.

         

carsten888

4:16 pm on Feb 5, 2011 (gmt 0)

10+ Year Member



When writing to a file, I noticed that some file which where utf-8, change to ANSI encoding (and thus becoming corrupt and useless).

This is the code I use to write to the file:


$fp = fopen($target_file, "wb");
fputs($fp, $string, strlen($string));
fclose ($fp);



I thought maybe the string itself might determen if the file becomes utf-8, like if string is chinese or japanese. But even writing something a simple 'soup' to the file makes it change from utf-8 to ANSI.

How to keep the file utf-8 when writing to it?

coopster

4:44 pm on Feb 8, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If the data is uploaded via form you can specify the accept_charset in the form element. PHP server side options for conversion including using a stream context [php.net] when opening [php.net] the file and/or possibly utf8-encoding [php.net] the data.

Additional resources:
mbstring functions, including mb_convert_encoding [php.net]

And this classic from the PHP Forum Library regarding Unicode Support [webmasterworld.com]

carsten888

10:27 am on Feb 21, 2011 (gmt 0)

10+ Year Member



This turns out to be a sligtly different problem altogether.

So far I have been using notepad to check if the file is utf-8. So I open the file, do file > 'save as' and check next to the save-button the encoding type in the selectbox. I thought that was the only way to check the encoding type on win7.

Someone told me to try notepad++. When creating a utf-8 file with the normal notepad, notepad++ recognizes it as utf-8. Then in notepad++ I encode it to UTF-8 without BOM (which I need for language files for Joomla 1.6). Then open the file in the normal notepad, save as and it says it is ANSI.

When I create a UTF-8 file with notepad++, then write to it with PHP, then open the file in notepad++, it still has the same encoding.

Which application to trust?
notepad
notepad++

coopster

5:13 pm on Feb 21, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



notepad on Windows is a very basic text editor. I would recommend using a text editor like notepad++ or textpad that allows you a lot more flexibility such as encoding type, line endings, etc. There are quite a few options when it comes to text editors so try a few out, see which fits your coding habits best.

carsten888

7:50 am on Feb 22, 2011 (gmt 0)

10+ Year Member



Actually for coding I use Macromedia Dreamweaver.

My problem is, how to make sure a file is really utf-8 when an application like the normal notepad says it is not.

coopster

5:09 pm on Feb 22, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I would like to believe Macromedia DW is advanced enough to show file properties, including charset type. However, as I mentioned in my last post, notepad is not. It is a basic text editor and nothing more. The "Save As" feature in notepad is not an indicator of the current charset of the file type you have open.