SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2006
    Posts
    410
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Text in Database - Html snippet

    Hey...im trying to source a simple snippet or script for inserting text into mysql that retains its format. Something very simple that will retain the users returns rather than adding <br><br> for every new paragraph. Font, links etc aren't needed to be editable...

    The database is updated via a html formatted form.

    If anyone can point me in the right direction it would be appreciated.

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, if you want it to display new lines as html, you have 2 choices.
    convert new lines to <br>, use the nl2br() function
    or output the text into <pre> tags

    btw- you dont need to store the text in the databse already converted to html line breaks, you can just do that on the output stage.

  3. #3
    SitePoint Enthusiast sarahG's Avatar
    Join Date
    Apr 2006
    Location
    Wirral, UK
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your query is how to store the detail or how to control the output?

    for storing, in the form where the text is entered just use a textarea for the main text/content and then store it as plain text. the newlines/paragraph breaks will automatically be stored in the db table.

    then for output as clamcrusher said, use nl2br() to insert line breaks where the newlines were.

  4. #4
    SitePoint Evangelist
    Join Date
    Mar 2006
    Posts
    410
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks both for your responses..
    Im making a very simple content management system for somebody, therefore im try to make it a easy as possible...i.e they can copy and paste text from a word doc into the html form to be posted into the database.

    What im after is a method that retains the para breaks in the html so that when the text record is pulled down into the webpage the para breaks remain. If i upload the html formatted text, its lost when stored in the database.

    If i use <br> or nl2br() function, doesnt the user still need to enter these in the text rather than just using the return key....or have i misunderstood?!?

    cheers

    b

  5. #5
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    make a form with a <textarea>

    enter some text, make a few lines in it, submit the form to a php script, then

    echo nl2br($_POST['textarea']);

    and look at the output.

    only difference is you would be using nl2br() on the data from the database. you wouldnt use it before you put the data in the database.

  6. #6
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Remember that copying from Word you will lose all formatting when pasting into a textarea except for the newlines (ENTER key). A paragraph equates to the <p></p> tag-pair ... in Word this may not necessarily result in 2 newlines.
    Ian Anderson
    www.siteguru.co.uk


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
  •