SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Guru
    Join Date
    Feb 2002
    Posts
    625
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    formatting text...

    Hello!

    Could anybody tell me how i can get the text that has been submitted through a form, using a <textarea>, back from the database in exactly the same way it has been submitted, into an <textarea>.

    A little example:

    I write like this

    into

    my <textarea> box.


    When i grab the data submitted to the database and display it in a textbox it looks like


    I write like this into my <textarea> box


    Any help would be appreciated.

    Thanks a lot in advance,
    datune

  2. #2
    Ribbit... Eric.Coleman's Avatar
    Join Date
    Jun 2001
    Location
    In your basement
    Posts
    1,268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Run it through htmlspecialchars first...
    Eric Coleman
    We're consentratin' on fallin' apart
    We were contenders, now throwin' the fight
    I just wanna believe, I just wanna believe in us

  3. #3
    SitePoint Guru
    Join Date
    Feb 2002
    Posts
    625
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *cough*
    You misunderstood me completely.

    Look, if you write in here, and submit your post, and you
    write
    like
    this
    in this
    forum.

    Now, after submitting the post, you can view your post and edit it. Now if you hit edit you will notice that the text will be displayed exactly the same way i have entered it now.

    My question, how can i do that?

    Thanks

  4. #4
    Ribbit... Eric.Coleman's Avatar
    Join Date
    Jun 2001
    Location
    In your basement
    Posts
    1,268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahh
    when you display it, run it through nl2br() then

    echo(nl2br($text));
    Eric Coleman
    We're consentratin' on fallin' apart
    We were contenders, now throwin' the fight
    I just wanna believe, I just wanna believe in us

  5. #5
    SitePoint Guru
    Join Date
    Feb 2002
    Posts
    625
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No no no no no!

    Argh, look, i don`t want any HTML stuff to be displayed at all.

    How can i format TEXT DISPLAYED INSIDE A TEXTAREA BOX.
    That is what i am looking for

    I want the text that i submit via a textarea box, which is stored in a DB to be displayed the same way i entered it inside the textarea box, in a textarea box.

    Enter Message in textarea box -->save in DB-->grab message from DB and display in the very same textarea the same way i have written it.

    There, i hope you will understand me now, if not i have to shoot me, cause i thought i knew how to write and talk in english :P

    Thanks a lot for your patience.

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you should do nothing special:

    <textarea name="blah">$textfromdb</textarea>

    whatever...i am guessing maybee you put <textarea value="<?=$variable?>"></textarea> and that would be wrong, it should be <textarea>DATA</textarea>
    but as long as you never change the line breaks (convert them to <br>) at the begining then everything should be ok.
    signature

  7. #7
    SitePoint Guru
    Join Date
    Feb 2002
    Posts
    625
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoaaa..*takes a huge sledgehammer and bumps it on his own head for being stupid*

    I solved it, typecero you hit the nail on the head.+

    I made the terrible error of storing the text formatted, ie. already having removed all the line breaks.

    Now this is the process, and i think its a good way to do.

    When storing data from i think you should always store it as is. Then when you want to display the text, have all linebreaks converted into <br/> using this
    PHP Code:
    $text preg_replace("/(\015\012)|(\015)|(\012)/","<br/>",$text); 
    Someone correct me if im totally wrong

    EDIT:
    Hey! My NULLS in front of 15 and 12 where not stored.
    Right so up there where the code is it should not be 12 and 15, but 012 and 015.
    Last edited by datune; Apr 9, 2002 at 22:14.

  8. #8
    SitePoint Enthusiast
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that is good but usually i use Kevin Yank's way:
    eregi_replace("\r", "", $string)
    eregi_replace("\n\n", "<P>", $string)
    eregi_replace("\n", "<BR>", $string)


    and to put the line breaks back:

    eregi_replace("\n\n", "<P>", $string)
    eregi_replace("\n\r", "<BR>", $string)
    something like that atleast (it is getting late)

    i use his because it is easier to read....and the fact that i dont know a damn ting about regular expressions helps...LoL
    signature

  9. #9
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    whatever you do, don't use Kevin Yank's way. nor is there the need for the preg_replace() on display. that's what nl2br() is for!

    there's a little extra tip to use before inserting text into the database in my PHP and MySQL coding tips thread, under the `Lines breaks' section. and then simply use nl2br() when displaying.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR


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
  •