SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: formating

  1. #1
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    formating

    Not to long ao, I asked how to format input so it includes line breaks (ie, when submitting a story with paragraphs, and such)

    Someone (I think it was 7stud) told me about nl2br().

    I tried using it, and I checked out the maual at php.net
    I couldnt seem to figure out how it worked, or at least how to use it. This is how I want to apply it,maybe you can help me.

    First I have a text box, someone puts their text in there all formated and such. Then they click submit, the data is stored to the database. I think this is where the text they entered is joined up into one long line of text, no line breaks. I then need to pull it out, I can do that using an array. Now I need to re-enter the line breaks somehow?
    understand what Im tryign to say?

    thanx

  2. #2
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    If they put line breaks into the text area then you use:

    $text = nl2br($text);

    However, if they didn't you may want to word wrap:

    $text = wordwrap($text, 65);
    $text = nl2br($text);


    You may not have to add that nl2br after the word wrap.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Don't use an array to pull it out. Just echo the field name using nl2br():

    echo nl2br($row['field_name']);

    There are two possibilities for your system: you have magic quotes turned on or you have magic quotes turned off. There are several characters that need escaping, including quotes and backslashes before inserting a string in a table. If magic quotes is on, then you don't have to escape characters before inserting them in the database: your system does that automatically for you.

    Newlines and carriage returns are represented by \r or \n or a combination of those two, so if you do not have magic quotes turned on, you have to escape the backslashes. In that case you would use the addslashes() function on the string before inserting into your table, and then use stripslashes() and nl2br() when displaying:

    echo nl2br(stripslashes($row['field_name']));
    Last edited by 7stud; Jun 3, 2001 at 12:15.

  4. #4
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    when you say brakes.. do you mean the actual tag <br> or do u just mean an actual line break where the text starts another line down

    also magic quotes arnt turned on

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    In that case you would use the addslashes() function on the string before inserting into your table, and then use stripslashes() and nl2br() when displaying:

    echo nl2br(stripslashes($row['field_name']));
    Which part of that didn't you understand?
    Last edited by 7stud; Jun 3, 2001 at 19:40.

  6. #6
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That works great, but its not taking out the escape slashes?
    It puts in the line breaks, but it dosnt take away the \ before things like " and \
    Last edited by slighltywhacked; Jun 3, 2001 at 21:57.

  7. #7
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If it is still displaying the \ before the special characters then obviously the stripslashes() function is not working. Try pulling out the data without stripping the slashes like 7stud said. Thus see what it does. If there is a \ before the special characters like there should be, put in the stripslashes() function again and see if it works.

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi fully,

    If you used addslashes() to enter the string in the table and stripslashes() to display it, and there are still slashes, what does that mean? addslashes() and stripslashes() negate each other making the string that comes out of stripslashes() the same as the string that went in to addslashes(), so what does that tell you if everything is escaped with slashes when you display it? Well, it tells me that you have magic quotes turned on, contrary to what you said. Magic quotes automatically escapes all the special characters with back slashes.

    So, as you were told the first time you posted this question (this being the second) and also in my previous post above, try entering the string in your database without doing anything to it, then when you display the string do this:

    echo nl2br($string);

    As I indicated above, there are only two options. When you have only two options, and one of them doesn't work, don't be afraid to try the other one to see if it works before you post another message.
    Last edited by 7stud; Jun 4, 2001 at 00:50.


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
  •