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 12 of 12
  1. #1
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts

    Maintaining line breaks in MySQL?

    Hello,
    I'm looking for a way to maintain line breaks when submitting a form <textarea> in php/mysql. I know in ASP i was able to do a replace() on the requested form data to keep the line breaks.

    can the same be done in PHP?


    thanks in advance for your time!
    -Ken

  • #2
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    you don't have to do anything tp preserve line breaks. if the user enters linebreaks on your form then the they will be there in your form processor when you insert into the database.

    be advised: different OS's have different ASCII chars for linebreaks:
    unix = \n
    windows = \r\n
    mac = \r
    in the incoming data will be different depending on the OS of the client.

  • #3
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    nl2br() This will convert all newlines to the <br /> tag

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    oh kool!
    and way less code then it took to do it in ASP

    now my next question in reverse.

    when bring the data back into a textarea for editing. how do i remove the <br /> and have just a blank space?


    thanks again!
    -Ken

  • #5
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    PHP Code:
    // to repalce with spaces
    $text str_replace('<br />'' '$text);

    // to replace with newlines
    $text str_replace('<br />'"\n"$text); 

  • #6
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    kool, that works too.

    i only have one problem now,
    everytime i click edit on the form, and seems to add an extra space inbetween each line break. why is this happening?

    -Ken

  • #7
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    beacuse nl2br() does not remove the original linebreaks, it simply adds <br> tags (the actual line breaks are still there). consider this example:
    PHP Code:
    <?php
    if (isset($_POST['submit']))
    {
        
    $text trim($_POST['text']);
        
    $text stripslashes($text);
        
    $text htmlspecialchars($text);
        
        echo 
    'you entered:<br><br>' nl2br($text);
    }

    echo 
    '
    <form action="" method="POST">
    enter text:<br>
    <textarea name="text">'
    ; if (isset($text)) {echo ($text);} echo '</textarea><br>
    <input type="submit" name="submit" value="submit">
    </form>
    '
    ;
    ?>

  • #8
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts

    Question

    ok, i'm back on this again, and still have the same problem,


    I don't understand the last example that devinemke showed me.
    should this be used in the script that pulls the data, or the script that submits the data to the DB?

    this is my code for pulls the data:

    PHP Code:
    function GetContent($edit) {

    $SqlHost "localhost";
    $SqlUser "root";
    $SqlPass "sql";
    $SqlDb     "National";

    $link mysql_connect($SqlHost$SqlUser$SqlPass) or die('Login details incorrect');
    $db_selected mysql_select_db($SqlDb$link) or die('Database name incorrect');

    $result mysql_query("SELECT cData FROM content where cid='$edit'");
    $row mysql_fetch_array$result );
    #echo $row['0'];
    $text str_replace('<br />'"\n"$row['0']);
    $text trim($text);
    $text htmlspecialchars($text);
    echo 
    $text;

    mysql_close($link);


    and it still pulls it with alot of spacing inbetween the each line break, and everytime i hit submit the spaces like double.

    my code for submitting to the DB:

    PHP Code:
    $content nl2br($content); 
    the rest is just standard sql stuff.

    how can I fix this?

    thanks again for all your time!
    -Ken

  • #9
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    you should store the data in the database with the regular linebreaks (\n) characters. you should only use nl2br() when displaying the data in the browser.

  • #10
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    yes, thats what i'm doing.
    but each time i edit a post and pull the data back down, it adds more line breaks.

    so, what am i doing wrong??

    -Ken

  • #11
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    ok, i found the prob.

    someone told me to use this when calling the text from the db.
    $text = str_replace('<br />', "/n", $row['0']);

    so i just removed the /n, and now it works correctly.

    thanks for all your time and help!

    -Ken

  • #12
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    ok, let's back up. run the data thru nl2br() only when it's being displayed in the browser as regular text, NOT when it is within a pre-populated form field. within a form field just leave it with the actual linebreak characters. after your form is submitted run the data your form validation code (trim() and whatever else) and then run your UPDATE query.

    in your GetContent() fucntion you posted above you are calling str_replace() to replace <br>'s with /n's which leads me to believe that you are storing <br>'s in the database (which you shouldn't).


  •  

    Posting Permissions

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