SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    returning text from a textarea

    Is there a way to take the value of a text area, and return all of the line breaks that were submited when the form was filled out.

    for instance:

    I type in this:

    "I come from a
    land down under.

    by men at work"

    and it is returned as this:

    "I come from a land down under.by men at work"

    I am using $_SESSION variables, to pass the information once it is recieved from the form if that changes things at all.

    thanks

  2. #2
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you submitting it into a DB?

    You can use

    Code:
    $Text = nl2br(trim($_POST['Text']));
    Tp place <br> after each line break before it gets submitted into a DB, or you can use something like

    Code:
    echo nl2br($text);
    to place <br> after each line when calling data up from a DB.

  3. #3
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Possibility
    Are you submitting it into a DB?

    You can use

    Code:
    $Text = nl2br(trim($_POST['Text']));
    If i use this, do i need to then use the other when i pull from the database, or are the <br>'s put in the database and remembered for return.

    ras

  4. #4
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the n12br is not a valid php or mySql function

  5. #5
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahhhh.....not n12br it is n2lbr heh sorry. got it fixed and it works thanks

  6. #6
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you use my first example when the data is being processed and placed in a DB, it will place the <br> in the DB, also. If you use my second example when you are displaying data from a DB, the <br>'s won't be placed in the DB (but the line spaces are still intact inside the DB, they just won't display as <br> unless you use that) then it only places the <br>'s while the data is being displayed.

    Anyways, you're welcome.

    EDIT: Ops, did I type that wrong? Sorry.

  7. #7
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok....i was just going to ask. Right now when i return any record, it shows up right, but when i return to update it, it has the <br> in it, which sucks, because then when i update without deleting those br's it adds more. so then i have<br><br>. whaich as you know is not going to work for me. so you think i should use the other way? it might make it easier to update.

  8. #8
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I run the cell data through nl2br when I'm printing to the screen, not writing to the database. As you mentioned, this makes it easier to edit.
    Mike
    It's not who I am underneath, but what I do that defines me.

  9. #9
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raskolnikov
    ok....i was just going to ask. Right now when i return any record, it shows up right, but when i return to update it, it has the <br> in it, which sucks, because then when i update without deleting those br's it adds more. so then i have<br><br>. whaich as you know is not going to work for me. so you think i should use the other way? it might make it easier to update.
    Use my second example then. Do not have it insert <br> when submitting it into the DB, rather, when calling the data, use

    PHP Code:
    echo nl2br('.$Row[text].'); 
    and when calling it back into a textarea for updating use

    PHP Code:
    <textarea name="body">'.$Row[text].'</textarea
    being in a textarea it will automatically have line breaks without using the nl2br.

    The above system works great for me.

  10. #10
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo nl2br('.$Row[text].'); 
    Wow, what a syntax it should be
    PHP Code:
    echo nl2br($Row['text']); 

  11. #11
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Gaheris
    Wow, what a syntax


    P.S. to all: the function is nl2br()--newline-to-break.

  12. #12
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    which is basically a function that converts \n to <br /> (For those who didn't read the manual )
    Mike
    It's not who I am underneath, but what I do that defines me.

  13. #13
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    UK
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nl2br() with the info on the way into the database and strip_tags() when you want to bring it back into the text area to edit it.....

  14. #14
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by djmattd
    nl2br() with the info on the way into the database and strip_tags() when you want to bring it back into the text area to edit it.....
    Disagree.
    Use nl2br() when printing to screen. Just as Naramation said.

    -Helge

  15. #15
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    which is basically a function that converts \n to <br />
    Disagree (yeah, I stole it from Helge).
    nl2br() doesn't replace \n, but rather adds the <br /> before it.
    nl2br -- Inserts HTML line breaks before all newlines in a string

  16. #16
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by djmattd
    nl2br() with the info on the way into the database and strip_tags() when you want to bring it back into the text area to edit it.....
    When bringing it into a textarea you do not need to nl2br()

  17. #17
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, because there aren't any <br /> tags in there if you do what Naramation and Helge have said.


Bookmarks

Posting Permissions

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