• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1051
  • Last Modified:

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,
  • 3
  • 2
1 Solution
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...
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now