Results 1 to 3 of 3

Thread: Adding Line breaks to a textarea box

  1. #1
    Join Date
    Oct 2006
    Posts
    94
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Cool Adding Line breaks to a textarea box

    I have tried preg_replace and str_replace but both show \n and \r within the textbox. I want to pull some data out of the database and display it in the textarea to allow for easy update.

    The data within one mysql field is seperated by a comma like so...

    data1,data2,data3....

    I want to replace the comma with a line break like so....

    data1
    data2
    data3....

    I have searched and searched but my search ends up with answers to the opposite! Removing line breaks for form submission. I can do that part!

    Hope someone can help!

    Regards

    Smithster

  2. #2
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    What exactly did you try?Does this work for you?
    PHP Code:
    preg_replace("/\,/""<br/>"$data

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    inside the text box, <br> would be displayed literally. \n, however, should do the trick - I tested this and it worked perfectly:
    PHP Code:
    print '<textarea>line one'."\n".'line two</textarea>'
    incidentially, this also worked:
    PHP Code:
    print '<textarea>one line
    two line</textarea>'

    though, obviously, you'll be using the newlines when you pull stuff from your database.

    Edit:

    I just caught what your problem really was; namely, your \ns and \rs weren't being parsed by php (they were treated as text instead).
    Try this (note the double quotes):
    PHP Code:
    $data 'line one,line two,line three';
    $ndata implode("\n",explode(',',$data));
    print 
    "<textarea>$ndata</textarea>"
    Edit:

    actually, that's all there was to it. this work fine too:
    PHP Code:
    $data 'line one,line two,line three';
    $ndata str_replace(',',"\n",$data);
    print 
    "<textarea>$ndata</textarea>"
    "Double Quotes FTW"



    Last edited by traq; 02-25-2011 at 04:18 AM.

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
  •