Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 3 of 3
  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    7
    Member #
    20129
    My site has a PHP based input page which creates a new file on the site. My problem is that when I submit something from a text area, PHP removes all the line breaks before adding it to a file. So that while the description I put in is properly formated, the one that viewers read is a single long paragraph. Is there any way of fixing that?

    Another problem is that if I put a " into the name of the file, fopen() automatically adds a backslash before it. (ie Bob's stuff becomes Bob/'s stuff)

    Anyone know why if you can stop either of these from happening?

    Thanks,
    Theodore

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,715
    Member #
    5580
    Liked
    717 times
    The backslash thing refers to this PHP command:
    $content = stripslashes($content);
    You can use that before you save it to the file.

    The line breaks is a little tougher to solve.
    This actually depends on how you edit the content.

    I usually do it this way .... but it's my personal preference.
    This is to save the content ... textarea from a form.
    PHP Code:
    <?php

    //  You have a whole textarea from a form ...
    //  The name of the form variable is "textarea".

    $textarea $_POST['textarea'];

    // Save the content 
    $url="page1.txt"// whatever you name the content text file
    $newtext=stripslashes($textarea); // remove the slashes  \'
    $newtext str_replace("<?"""$newtext); // remove any PHP script references
    $newtext str_replace("?>"""$newtext); // remove any PHP script references
    $newtext nl2br($newtext); // Change all "new line" carriage returns to <br />
    // open the text file and save it all.
    $fh fopen($url'w') or die("can't open file");
    fwrite($fh$newtext);
    fclose($fh); 
    header ("location: index.php"); // return back from where you came

    ?>
    Now, when you display it for editing (in a textarea on a form), you need
    to convert the <br /> back to "new lines"...

    PHP Code:
    <?php

    // Your text file
    $url "page1.txt";

    // Get page
    $data implode(""file($url)); 

    // Convert all <br> to "new lines".
    // There is not a built-in PHP function to do this, so I call my own function (see below).
    $newdata br2nl($data);

    // form stuff here ....
    // the <br>'s disappear and now become textarea "new lines" or "carriage returns".
    echo "<textarea name='textarea' rows='25' cols='80'>$newdata</textarea>";


    // A function to convert <br> and <br /> back to "new lines".
    function br2nl($str) {
    return 
    preg_replace('=<br */?>=i'""$str);
    }

    ?>


  4. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    7
    Member #
    20129
    Thanks,
    That works great.
    Theodore


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 12:57 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com