How to set encoding for fwrite?

Posted on 2005-04-26
Last Modified: 2012-06-27
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,
Question by:niddocks
    LVL 6

    Accepted 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)


    Author Comment

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

    Thanks for trying.
    LVL 6

    Expert Comment

    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...

    Author Comment

    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.
    LVL 6

    Expert Comment

    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?
    LVL 14

    Expert Comment

    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

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now