Latze
Latze

Reputation: 1871

File I/O to textarea in PHP

My friend and I have a little spare time home page together. He's not a programmer, and in order for him to be able to change some text on the front page, I created a php-script that

1) Reads data from file "tester.txt" (this is the text that should go on the front page)
2) Prints this text to a textarea, where you can edit the text and submit it again
3) Writes the edited text to the same file, "tester.txt"

The two functions Read(); and Write(); look like this

function Read() {
    $file = "tester.txt";
    $fp = fopen($file, "r");
    while(!feof($fp)) {
        $data = fgets($fp, filesize($file));
        echo "$data <br>";
    }
    fclose($fp);
}

function Write() {
    $file = "tester.txt";
    $fp = fopen($file, "w");
    $data = $_POST["tekst"];
    fwrite($fp, $data);
    fclose($fp);
}

The only problem I have is that when the text is printed to a text area the line returns are written as <br> - and I don't really want it to do that, because when you edit some of the code and rewrites it, another layer of <br>'s appear. Here's a screenshot to illustrate:

Test 1

Test 2

Is there any workaround to this?

Thanks!

If you need the rest of the code, here it is:

<html>
    <head>
        <title>Updater</title>
    </head>
    <body>
        <?php
               function Read() {
                   $file = "tester.txt";
                   $fp = fopen($file, "r");
                   while(!feof($fp)) {
                       $data = fgets($fp, filesize($file));
                       echo "$data <br>";
                   }
                   fclose($fp);
               }

               function Write() {
                   $file = "tester.txt";
                   $fp = fopen($file, "w");
                   $data = $_POST["tekst"];
                   fwrite($fp, $data);
                   fclose($fp);
               }
        ?>

        <?php
        if ($_POST["submit_check"]){
            Write();
        };
        ?>      

        <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
        <textarea width="400px" height="400px" name="tekst"><?php Read(); ?></textarea><br>
        <input type="submit" name="submit" value="Update text">
        <input type="hidden" name="submit_check" value="1">
        </form>

                <?php
        if ($_POST["submit_check"]){
            echo 'Text updated';
        };
        ?>      


    </body>
</html>

Upvotes: 3

Views: 15982

Answers (6)

masnun
masnun

Reputation: 11906

This is happening because while writing the contents to the text area, you're putting a <br> at the end of each line. But in a textarea line breaks are noted by "\n". When you're saving your existing text, the next time the line breaks are replaced with more <br>.

While printing out the content on a public page, keep the
. But in the editing page, remove the br.

Here's what I would have done with the PHP code:

<?php
    define("FILE_NAME", "tester.txt");

    function Read()
    {
        echo @file_get_contents(FILE_NAME);
    }

    ;

    function Write()
    {
        $data = $_POST["tekst"];
        @file_put_contents(FILE_NAME, $data);
    }

    ?>

    <?php
    if ($_POST["submit_check"])
    {
        Write();
    }
    ?>      

Upvotes: 2

nickb
nickb

Reputation: 59699

This is simpler than you think. You shouldn't be outputting the <br> tags as the textarea already contains the entered newline characters (\r\n or \n). You don't have to read the file like that, if you read it this way, you never have to worry about the character contents.

Change:

$fp = fopen($file, "r");
while(!feof($fp)) {
    $data = fgets($fp, filesize($file));
    echo "$data <br>";
}
fclose($fp);

to:

echo file_get_contents( $file);

Problem solved.

Upvotes: 12

Pastor Bones
Pastor Bones

Reputation: 7351

Use regex to replace the break tag with a newline character

preg_replace('#<br\s*/?>#i', "\n", $data);

You can find a more detailed explanation answered here

Upvotes: -1

cocoahero
cocoahero

Reputation: 1302

You don't need the <br>'s. Depending how your <textarea> is configured, by default lines are hard wrapped using \n's. These are preserved when you save the file, therefore you don't need to add your own line breaks.

Upvotes: 0

DaveRandom
DaveRandom

Reputation: 88647

At a guess, this is hosted on a *nix machine, and he is using a Windows machine to do the editing?

If this is the case, changing your write function to this should solve the problem:

function Write(){
    $file = "tester.txt";
    $fp = fopen($file, "w");
    $data = str_replace(array("\r\n","\r"),"\n",$_POST["tekst"]);
    fwrite($fp, $data);
    fclose($fp);
};

Upvotes: 0

Tom van der Woerdt
Tom van der Woerdt

Reputation: 29975

You use echo "$data <br>"; - just make that echo $data; ?

Upvotes: 0

Related Questions