[newbie]how can I create a utf-8 files with php?

Discussion in 'PHP' started by shedokan, Dec 7, 2007.

  1. #1
    I tried to create a utf-8 file with this script:

    $f = fopen($_POST["id"].".inc", "wb"); 
    $text = "\xEF\xBB\xBF".$_POST["text"]; 
    fwrite($f, utf8_encode($text));  
    fclose($f);
    Code (markup):
    but all I get is this:
    ×’×’×›×’×›×’×’×›×›×’×’×›×›×’
    Code (markup):
    thanks in advance.
     
    shedokan, Dec 7, 2007 IP
  2. Mr_CoWz

    Mr_CoWz Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    the is an error with ur script, jus check over it again
     
    Mr_CoWz, Dec 7, 2007 IP
  3. shedokan

    shedokan Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    it has no errors I go to that page after a form.
    here's the whole script:
    <?php
    if(isset($_POST["id"])){
    $f = fopen($_POST["id"].".inc", "wb"); 
    $text = "\xEF\xBB\xBF".$_POST["text"]; 
    fwrite($f, utf8_encode($text));  
    fclose($f); 
    } else {
    ?>
    <form action="create.php" method="POST">
    <textarea rows="15" cols="50" name="text" id="text"></textarea><br>
    <input type="text" name="id" value=""><br>
    <input type="submit" value="create file">
    </form>
    <?php
    }
    ?>
    Code (markup):
     
    shedokan, Dec 7, 2007 IP
  4. Mr_CoWz

    Mr_CoWz Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    soz my mistake
     
    Mr_CoWz, Dec 7, 2007 IP
  5. codesome

    codesome Peon

    Messages:
    98
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #5
    may be:
    
    $f = fopen($_POST["id"].".inc", "wb"); 
    $text = $_POST["text"]; 
    fwrite($f, "\xEF\xBB\xBF".utf8_encode($text));  
    fclose($f);
    
    PHP:
     
    codesome, Dec 7, 2007 IP
  6. shedokan

    shedokan Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    nope now this is what is inthe file I create:
    ãâãâããââããâ
    Code (markup):
     
    shedokan, Dec 7, 2007 IP
  7. codesome

    codesome Peon

    Messages:
    98
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Do you know that utf8_encode() encodes an ISO-8859-1 string to UTF-8?
    It seems that you use not an ISO-8859-1 encoding.
     
    codesome, Dec 8, 2007 IP
  8. shedokan

    shedokan Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    ow thanks.
    and here is the code who solved my problem:
    <?php
    function writeUTF8File($filename,$content) {
            $f=fopen($filename,"w");
            # Now UTF-8 - Add byte order mark
            fwrite($f, pack("CCC",0xef,0xbb,0xbf));
            fwrite($f,$content);
            fclose($f);
    }
    ?>
    PHP:
     
    shedokan, Dec 8, 2007 IP
  9. codesome

    codesome Peon

    Messages:
    98
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #9
    The same algorithm, i gave you, but mine is faster.
     
    codesome, Dec 8, 2007 IP
  10. shedokan

    shedokan Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    yes but I se hebrew text.
    anyway's d you know how to remove the BOM(Bite Order Marker)?
     
    shedokan, Dec 8, 2007 IP
  11. codesome

    codesome Peon

    Messages:
    98
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Just remove first 3 bytes from string. Implementation depends on your files size.
    
    $a=file($fname);
    $a=implode($a);
    $a=substr($a,3);
    
    PHP:
     
    codesome, Dec 8, 2007 IP
  12. shedokan

    shedokan Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    you know what, I just found out I don't need to remove the BOM.
     
    shedokan, Dec 9, 2007 IP