Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Oct 2007
    Posts
    277
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Question how to create a utf-8 or a unicode file with php?

    I tried to create a utf-8 file with this script:
    Code:
    $f = fopen($_POST["id"].".inc", "wb"); 
    $text = "\xEF\xBB\xBF".$_POST["text"]; 
    fwrite($f, utf8_encode($text));  
    fclose($f);
    but all I get is this:
    Code:
    גגכגכגגככגגככג
    Last edited by shedokan; 12-07-2007 at 04:27 PM.

  • #2
    Regular Coder
    Join Date
    Oct 2007
    Posts
    277
    Thanks
    2
    Thanked 4 Times in 4 Posts
    I solved it!
    here's my code:
    PHP Code:
    <?php
    function writeUTF8File($filename,$content) {
            
    $f=fopen($filename,"w");
            
    # Now UTF-8 - Add byte order mark
            
    fwrite($fpack("CCC",0xef,0xbb,0xbf));
            
    fwrite($f,$content);
            
    fclose($f);
    }
    ?>

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,472
    Thanks
    0
    Thanked 634 Times in 624 Posts
    If your content is actually UTF then the byte order mark should be written automatically. Adding a byte order mark doesn't convert the character set the rest of the content is written in to UTF8
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    just want to shout a big thankyou to Shedokan for sharing his code! i couldn't fwrite chinese characters into a new file but with this i could now. it helped me a lot. thank you!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •