Page 1 of 5 123 ... LastLast
Results 1 to 10 of 47

Thread: textarea using ENTER

  1. #1
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default textarea using ENTER

    hi. I have textarea whish i use to store max 500 characters. What i want?
    I want to save ENTER push's, because when my output comes, I got all written in one line. But user pushed enter to start writing from new line... If you need more info - ask. Thanks a lot
    Last edited by auriaks; 11-22-2009 at 09:34 AM.

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    That's because the key for enter comes back as "\n", so you have to replace all of those with "<br />" tags.

    Here you go:

    PHP Code:
    $text str_replace('\n''<br />'$_POST['textarea']); 
    Remember to escape the text as well, if you're going to be inserting this into a database.

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,091
    Thanks
    96
    Thanked 98 Times in 96 Posts

    Default

    Actually, returns are
    Code:
    \r\n
    , but Schmoopy's code will still work just fine. I'm just being nit picky
    Last edited by james438; 11-16-2009 at 12:58 AM. Reason: grammar fix
    To choose the lesser of two evils is still to choose evil. My personal site

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Actually the type of line break produced by the enter key depends upon the OS:

    • Windows - CR LF (0d0a)
    • 'nix - LF (0a)
    • Mac - CR (0d)


    The Carriage Return (CR) is represented in a regular expression by either \r or \x0d, the Line Feed (LF) by either \n or \x0a.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Thanks for pointing that out guys

    A quick question then. Based on what you've said, the code I currently put up would only count as a line break in Windows and Linux, but not in Mac?

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I would tend to think so. However, it is unclear to me if, since this is on the server side, whether the server would convert to its method. In any case though, if one used an or operator (|) one should be OK, something like (I'm not certain of PHP regular expression syntax, or what the order of precedence would be here):

    Code:
    '(\r\n|\r|\n)'
    in place of:

    Code:
    '\n'
    as long as that doesn't result in the Windows style line break generating two <br> tags for each. If that happened, then one would have to also employ negative look ahead and negative look behind for the single character portions of the expression.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    There is a function in PHP that already does this and it is called nl2br()

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    nl2br — Inserts HTML line breaks before all newlines in a string
    That would leave out Mac OS's. However, there is a strategy in the comments section of that page on nl2br that is just what I was talking about.

    PHP Code:
    function Only1br($string

        return 
    preg_replace("/(\r\n)+|(\n|\r)+/""<br />"$string); 

    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    Thanks guys... Also it was interesting to read all the information about OS's. Btw, is is hard to create textarea like in this forum? with insertings php bold italic size style etc? maybe someone have links, tutorials. Any info is welcome. Thanks
    Last edited by auriaks; 11-16-2009 at 12:15 AM.

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by auriaks View Post
    Btw, is is hard to create textarea like in this forum? with insertings php bold italic size style etc?
    I was thinking that something like that might be useful for what you are trying to do. It requires javascript enabled on the user's end. I found that out recently when I disabled javascript and tried posting here. I was able to, but none of the editor's features were available.

    You should look here:

    http://www.dynamicdrive.com/dynamici...itor/index.htm

    it (also requires javascript) may be a good adjunct to your current project.

    Also, if you already have a forum package, it may include an option for this sort of thing. The vBulletin package used by DD includes this javascript powered input as an option, but it must be configured to do all of what the current 'editor' here does.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  11. The Following User Says Thank You to jscheuer1 For This Useful Post:

    galfrid (11-18-2009)

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
  •