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.
Page 1 of 3 123 LastLast
Results 1 to 15 of 41
  1. #1
    New Coder
    Join Date
    Aug 2009
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts

    PHP write to txt: special characters

    I am trying to get a very simple guestbook working, without using MySQL. Submitting a message (through a form) calls a php-script that adds the new entry to a plain txt-file. It works, but special characters such as "é" don't work, they become something like "é". I have found that this can be solved with utf-8 encoding, but I didn't manage to get this to work yet.

    I am only using and adjusting a free available script, I don't know too much about php myself.

    The form:
    PHP Code:
    <form action="act.php" method="post">
     <
    p>Name<br />
       <
    input name="name" type="text" class="textfield" value="" size="20" /></p>
     <
    p>Message<br />
       <
    textarea class="textarea" rows="4" cols="30" name="message"></textarea></p><br />
     <
    input type="submit" name="send" value="Send" />
     <
    input type="reset" name="reset" value="Reset" />
    </
    form
    Relevant parts of act.php:
    PHP Code:
    <?php
    function clean($str) {
        
    $str stripslashes $str );
        
    $str htmlentities $str );
        
    $str strip_tags $str );
        
    $str utf8_encode $str );
        return 
    $str;
    }
    // Get the data from the 'Name' field of the form.
    $data '<b>'.$_POST['name'].'</b>'.'<span class="date"> (' date('d.m.Y H:i') .') </span>'.'<br />';
    // Get the data from the 'site' field of the form.
    // $data = $data.'<b>Web site: </b>'.'<a href="http://'.$_POST['site'].'">'.$_POST['site'].'</a>'.'<br />';
    // Get the data from the 'Message' field of the form.
    $data $data.'<p><span class="gbp">'.clean($_POST["message"]).'</span></p><hr />';  

    // The file to write $data to (don't forget to CHMOD it to 666).
    $file "messages.txt";   
    $new stripslashes($data) . "\n" implode'' file$file ) );


    if (!
    $file_handle fopen($file,"w")) 

    echo 
    "Cannot open file"
    }  
    if (!
    fwrite($file_handle,utf8_encode($new)))

    echo 
    "Cannot write to file"
    }     
    fclose($file_handle);  

    echo (
    '<meta http-equiv="Refresh" content="0;url=bedankt_gb.php" />');
    }
    }
    ?>
    For example, with two test entries, the messages.txt file looks like this:
    Code:
    <b>Tom</b>
    <span class="date"> (22.08.2009 13:16) </span><br />
    <p><span class="gbp">h&Atilde;&copy;h &Atilde;&copy; test h&Atilde;&uml;</span></p>
    <hr />
    <b>Tom</b>
    <span class="date"> (22.08.2009 13:13) </span><br />
    <p><span class="gbp">h&Atilde;&copy; hehe ok @ tom</span></p>
    <hr />
    If anyone could help, thanks!

  • #2
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    My advice being remove all diff functions that you've used for encoding and use this when loading/defining your data, i.e.

    $data = blabla;

    to


    PHP Code:
    $data blabla;
    $data mb_convert_encoding($data'HTML-ENTITIES'"UTF-8"); 
    and use the same just before you write to your file.

  • #3
    New Coder
    Join Date
    Aug 2009
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I seem to have a problem with that function:

    "Fatal error: Call to undefined function: mb_convert_encoding() in /home/dbvaxel/public_html/act.php on line 14".

  • #4
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    could you repost your code with the alterations?

  • #5
    New Coder
    Join Date
    Aug 2009
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Hm, I reverted it and kind of messed up the code.

    Do I have to remove the cleaning function as well (if I drop the utf-encoding there, adding it where you suggest) or do I have to keep this?

    PHP Code:
    function clean($str) {
        
    $str stripslashes $str );
        
    $str htmlentities $str );
        
    $str strip_tags $str );
        return 
    $str;


  • #6
    New Coder
    Join Date
    Aug 2009
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Perhaps it's not the most efficient way to try and fix my current code since that is already a mix of various things I tried and partial solutions I found online. For more experienced php-users, I guess it's not hard to do this in a few lines. I'll try to explain what needs to happen, hopefully this is something that can be done easily.

    The information for a new entry comes from the form I gave in my first post; it contains "name" and "message". It is sent to act.php, which needs to process the new entry. For a new entry, I want the following expression added to the top of the messages.txt file (which I saved in notepad with "UTF-encoding"):

    <b>name</b><span class="date"> (date) </span><br /><p><span class="gbp">message</span></p><hr />

    The variables in bold need to be the data from the form (supporting special characters), the date in italic should be added with a php-function. The contents of messages.txt is just read by a html file, styled via css.

    Because the standard php-functions that write to a file add to the bottom of the file (or top, but then they overwrite), I use the following trick: store the current contents of messages.txt in a variable and adding the new entry to this variable, with ($data refers to the data of the new entry):

    PHP Code:
    $file "messages.txt";   
    $new stripslashes($data) . "\n" implode'' file$file ) ); 
    Because I've been adding encode-utf functions in all kinds of places, it may be better to start from scratch. Thanks in advance!

  • #7
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    <?php 
    function clean($str) { 
        
    $str strip_tags $str ); 
        return 
    $str

    // Get the data from the 'Name' field of the form. 
    $data '<b>'.$_POST['name'].'</b>'.'<span class="date"> (' date('d.m.Y H:i') .') </span>'.'<br />'
    // Get the data from the 'site' field of the form. 
    // $data = $data.'<b>Web site: </b>'.'<a href="http://'.$_POST['site'].'">'.$_POST['site'].'</a>'.'<br />'; 
    // Get the data from the 'Message' field of the form. 
    $data $data.'<p><span class="gbp">'.clean($_POST["message"]).'</span></p><hr />';   

    // The file to write $data to (don't forget to CHMOD it to 666). 
    $file "messages.txt";    
    $new stripslashes($data) . "\n" implode'' file$file ) ); 


    if (!
    $file_handle fopen($file,"w"))  
    {  
        echo 
    "Cannot open file";  
    }   
    if (!
    fwrite($file_handle,utf8_encode($new))) 
    {  
        echo 
    "Cannot write to file";  
    }      
    fclose($file_handle);   

    echo (
    '<meta http-equiv="Refresh" content="0;url=index.html" />'); 

    ?>
    PHP Code:
    <form action="act.php" method="post"> 
     <p>Name<br /> 
       <input name="name" type="text" class="textfield" value="" size="20" /></p> 
     <p>Message<br /> 
       <textarea class="textarea" rows="4" cols="30" name="message"></textarea></p><br /> 
     <input type="submit" name="send" value="Send" /> 
     <input type="reset" name="reset" value="Reset" /> 
    </form>  

    <?php

    $file 
    file_get_contents("messages.txt");
    $file mb_convert_encoding($file'HTML-ENTITIES'"UTF-8"); 
    echo 
    $file;
    ?>
    Just see if this works as i think this is what your after.

  • Users who have thanked Phil Jackson for this post:

    TomD (08-23-2009)

  • #8
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    just save them in a new folder and seperate files - act.php and index.php

  • #9
    Regular Coder
    Join Date
    Nov 2007
    Location
    Leeds, UK
    Posts
    514
    Thanks
    24
    Thanked 19 Times in 19 Posts
    I'm i missing some thing php can handle special characters in quotes E.G
    "©" the only problem is HTML cant output it if you saving to a TEXT file just use the quotes and the char,

    If thats the case dont use UTF8

    htmlspecialchars()

    PHP Code:
    <?php
    $file 
    file_get_contents("messages.txt");
    $file htmlspecialchars($file);
    echo 
    $file;
    ?>
    Last edited by barkermn01; 08-22-2009 at 05:55 PM.
    Working towards a Internet where we don't have website just browser applications Kill the Hyper-link and say hello to 3D Games in the browser :)

  • #10
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    sorry this one

    PHP Code:
    <form action="act.php" method="post"> 
     <p>Name<br /> 
       <input name="name" type="text" class="textfield" value="" size="20" /></p> 
     <p>Message<br /> 
       <textarea class="textarea" rows="4" cols="30" name="message"></textarea></p><br /> 
     <input type="submit" name="send" value="Send" /> 
     <input type="reset" name="reset" value="Reset" /> 
    </form>  

    <?php

    $file 
    file_get_contents("messages.txt");
    $file utf8_decode($file);
    echo 
    $file;
    ?>

  • Users who have thanked Phil Jackson for this post:

    TomD (08-23-2009)

  • #11
    New Coder
    Join Date
    Aug 2009
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thanks Phil. This seems to be partially working, I think I didn't do the "decode" step when outputting the messages.txt file. But now, only the last added entry is correctly displayed. When adding a new one, the old ones lose the correct special characters. Do I have to add some decode/encode-step when storing the old contents in the file-variable to add a new entry?

  • #12
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    can you show me the scripts that you have now altered and the message.txt (if possible) and will take a look.

  • #13
    New Coder
    Join Date
    Aug 2009
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Alright, I started from a clean (empty) messages.txt file, saved in UTF-8-format with notepad. I'm using the exact php-code you posted as act.php. I'm also using your (last) piece of code to show the entries. So I assume I don't need to copy them, I didn't make any alterations.

    I first added: Tétrisö. This is then shown correctly and messages.txt looks like this:

    <b>Tom</b><span class="date"> (23.08.2009 12:36) </span><br /><p><span class="gbp">Tétrisö</span></p><hr />

    Although the signs are "crazy" here, the output is shown correctly as "Tétrisö" on the site. Then I added "Tétrisö" again as a second entry. Now only this new message is shown correctly, the first one is again with "crazy signs". The contents of messages.txt is now:

    <b>Tom2</b><span class="date"> (23.08.2009 12:40) </span><br /><p><span class="gbp">Tétrisö</span></p><hr />
    <b>Tom</b><span class="date"> (23.08.2009 12:36) </span><br /><p><span class="gbp">Tétrisö</span></p><hr />

    It is shown in the output as:

    Tom2 (23.08.2009 12:40)
    Tétrisö
    Tom (23.08.2009 12:36)
    Tétrisö

    My guess is that when the old entries are read from messages.txt and stored in a variable where the new entry is added, also some en- or decoding needs to be done?

  • #14
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    am i correct in assuming you altered your function to:

    PHP Code:
    function clean($str) {  
        
    $str strip_tags $str );  
        return 
    $str;  


  • #15
    New Coder
    Join Date
    Aug 2009
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Yes, I'm now using your exact code of this post for act.php.


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

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