SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I use something like this for inserting documents into my SQL database:

    $body = nl2br($body);

    Now, this is all well and good - it takes my paragraphs and displays them as such when stuck onto a plain old page. The only problem, however, is modifying them.

    When I modify them, the database now contains "<br> <br>" in place of the paragraphs, and displays this HTML inside the box I use to modify my documents.

    I tried using e_regi replace to replace every instance of "<br>" with "\n", but it doesn't seem to be working. Any advice?

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2000
    Location
    Allentown PA
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nl2br replaces /n with <br>/n, so if I'm understanding you, you just want to remove the <br>'s. (Those are actually backslashes, but they get stripped out)

    $body = str_replace('<br>', '', $body);

    Another option is to not do the nl2br before inserting it into the db, but rather to do it after retrieving the record and just before displaying it.
    <Edited by Ed Sullivan on 01-26-2001 at 09:38 PM>

  3. #3
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Ed,

    I'm using this now:

    $body = eregi_replace("<br>"," ",$body);

    Unfortunatly, however, I'm getting twice the line breaks expected. I really have no idea why, though.

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2000
    Location
    Allentown PA
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm... I just ran this test:

    Code:
    <?php
    
    $string = "line 1\nline 2\n\nline 4
    line 5
    
    line 7";
    
    echo "---\n$string\n---\n<P>\n";
    
    $string2 = nl2br($string);
    echo "---\n$string2\n---\n<P>\n";
    
    $string3 = str_replace('<br>','',$string2);
    echo "---\n$string3\n---\n<P>\n";
    
    ?>
    And it output this:
    Code:
    ---
    line 1
    line 2
    
    line 4
    line 5
    
    line 7
    ---
    <P>
    ---
    line 1<br>
    line 2<br>
    <br>
    line 4
    <br>
    line 5
    <br>
    
    <br>
    line 7
    ---
    <P>
    ---
    line 1
    line 2
    
    line 4
    line 5
    
    line 7
    ---
    <P>
    Which seems to be correct. What happens when you use str_replace?

    Also, just want to test this backslash issue because I just asked Wayne and he said he only added one line of code:
    \ \\ \\\ \\\\ \\\\\ \\\\\\
    Hmm... no output.

  5. #5
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I havn't used str_replace() - I'm only mildly familiar with it...

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2000
    Location
    Allentown PA
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try it. It's significantly faster than any regex anyway.
    It's case sensitive, yes, but nl2br only generates one case of <br>'s.

  7. #7
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dang, I used $body = str_replace("<br>","",$body);, but it doesn't work. I really don't get it - I was sure the last solution would have done it.

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Chris I think what Mike said is the best solution. Don't use nl2br until pulling th data out for viewing on the we b page. that you will always have it in a format that can be edited without having to worry about the <br> because they will never actually get stored with the text in the database, you would only see them on output.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  9. #9
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Will that work? I find that if I insert it into a database like that, it just turns it into one big massive paragraph - how will the function then know where the paragraphs are?

  10. #10
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use $var = addslashes($var) before inserting it into the database and $var = stripslashes($var) when insertining into a from field for editing.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  11. #11
    SitePoint Enthusiast
    Join Date
    Nov 2000
    Location
    Allentown PA
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by freddydoesphp
    use $var = addslashes($var) before inserting it into the database and $var = stripslashes($var) when insertining into a from field for editing.
    Yes, you're always going to want to run addslashes:
    $var = " 'hello world' ";
    mysql_query("INSERT INTO table VALUES ('$var')");

    INSERT INTO table VALUES (' 'hello world' ') isn't valid.

    This is how vBulletin works I should add.

  12. #12
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Uh, I don't understand - I asked something very different, but thank you anyway - I am, thankfully, familiar with those two functions.

  13. #13
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Chris if you run addslashes before inserting the data into the db it will preserve the line breaks, when you pull it out and stick into say a textarea if you don't run stripslashes it will essentialy double the amount of spaces, don't ask me why but it does work.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  14. #14
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Odd. Thanks Freddy/Mike, I'll have to try that out.

  15. #15
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tried it out - it works for the modification in the textarea form field, but the line breaks are ignored elsewhere - meaning I still have to use nl2br() and put up with the <br> tags showing up.

    I'm totally stumped - ack, this is frustrating!

    The only thing I can get going is to use this:

    $body = eregi_replace("<br>","\n",$body);

    It produces two newlines for every <br> now...I can't fathom why.


  16. #16
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not just use add and stripslashes() for inserting or editing the data but when you disply it in html on the web page use nl2br that way it doesn't have the <br>'s stuck in until display on the web page?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  17. #17
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Uh, because I'm stupid.

    Thanks man! I think it all works the way I'd wanted now. That'd been bugging me for the longest time. I've no idea why the addslashes()/stripslashes() functions do that...but hey, it works.

  18. #18
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I know why because by addslashes you preserve the line breaks if you were to pull the data out then put in a textarea and then edit them and addslashes again ti would double your linebreaks so you need to strip them before you add them again, does that make sense?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  19. #19
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dunno. Whatever it is, it works though.


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
  •