How to set encoding for fwrite?

I need to create a file, which should contain special Polish characters (such as ś) but the file is created UTF-8 encoded when perforing fwrite. I import this file to a ISO-8859-2 encoded html
but currently the page appears incorrectly due to the character set mismatch and displays ś.

Is there a way I can set encoding for php fwrite so that my file is save ISO-8859-2 encoded? Am I on the right path or is there a better way to do it.

$fhandle = fopen( 'something.txt', 'w');
fwrite( $fhandle, 'Polish Character: ś');
fclose( $fhandle );

Thank you,
Who is Participating?
You can do this using multibyte string php extension:

// $someUTFdata - is text you want to convert
$someData = mb_convert_encoding($someUTFdata, "ISO-8859-2", "UTF-8");


fwrite($fp, $someData)

niddocksAuthor Commented:
:-( Nice try German Rumm. It looked so good, but didn't work unfortunately.

Thanks for trying.
try this:
$somedata = html_entity_decode($someUTFdata), ENT_COMPAT, 'ISO-8859-2');

html_entity_decode() will convert all HTML representations of symbol back to characters.
ä will become ä, and so on...
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

niddocksAuthor Commented:
html_entity_decode() works fine for when I want to display the characters to a web page in HTML, but for some reason it doesn't work at all when I write it to a text file.

Thanks for trying again.
Explain your problem in more detail?

I created files in Estonian and Russian languages with no problem at all using this.
Where do you get your data from? From $_POST/$_GET or from database?
Where do you view the resulting file - in text editor or in web-browser?
Why do you think text file is UTF-8 encoded?
No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: German_Rumm {http:#13866868}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

EE Cleanup Volunteer
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.