[PHP] How to Retain <BR>?

Discussion in 'Web Design & Coding' started by madmatt, Jul 30, 2006.

  1. madmatt

    madmatt Bow Down to the King Political User

    Messages:
    13,312
    Location:
    New York
    Okay, I feel really dumb for not knowing this but I can't figure out what I am doing wrong.

    When I add new content to my blog I am using the following MySQL statement:

    Code:
    $addArticle		= nl2br(trim(addslashes($_POST['blogformArticle'])));
    
    When I update content on my blog I am using the following statement:

    Code:
    $updateArticle	= nl2br(trim(addslashes($_POST['blogformArticle'])));
    
    When I display the content I use the following statement:

    Code:
    $viewArticle	= stripslashes($viewRow["article"]);
    
    However, it won't display it with the <br />'s in place. This is the HTML code:

    Code:
    Microsoft said on Friday that it is recalling an update to its Small Business Server product because of a glitch found late in the manufacturing process. <br />
    <br />
    The software maker said it found a problem with Windows Small Business Server 2003 R2 after the product was released to computer makers but before it was made broadly available. Small Business Server is a product that combines the Windows Server operating system with the Microsoft Exchange e-mail server and other software. The R2 release is an update to the version that was finished in 2003. <br />
    <br />
    "Recently, and during a regular audit as part of our software production process, Microsoft became aware of an issue with the final...software containing nonfinal versions of a few core components," Microsoft said in a statement provided to CNET News.com. "Since Microsoft has only just released SBS 2003 R2 to our manufacturing partners (OEMs, system builders and distributors), and it is not yet generally available to customers, the scope of this concern for partners and customers is very limited." <br />
    <br />
    Microsoft said it was recalling and would reissue the software, and general availability of the product would see a "minor delay." <br />
    <br />
    "Our customers and partners are our first priority, and while a short delay in availability of SBS 2003 R2 is unfortunate, delivering the highest-quality Small Business Server product to our customers and partners is the right thing to do," Microsoft said. <br />
    <br />
    One analyst said the fact that a glitch can show up so late in Microsoft's manufacturing process shows the challenges the software maker faces as its software becomes ever more complex. That challenge is heightened by the fact that the company no longer relies on burning all of its products to CDs; it doesn't have the luxury of taking that time to find bugs. <br />
    <br />
    "This isn't really a stellar advertisement for Microsoft's plan to roll out more and more complex software through automatic update," Directions on Microsoft analyst Rob Helm said. <br />
    <br />
    While this glitch affected just a few people, Helm said, a problem with something going out over Automatic Update could potentially affect millions of people. Microsoft said earlier this week that it would use the autoupdating utility to deliver Internet Explorer 7 for Windows XP.<br>
    
    None of the blank lines appear and I don't know what I am doing wrong. Technically, there should be a blank line in between each paragraph, the <BR /> is there after all.

    Any help is appreciated. Thank you.
     
  2. albybum

    albybum Penguin Rancher

    Messages:
    280
    Location:
    Elizabethton, TN
    I do not see anything in the above-posted code that would strip off <br />.

    nl2br()
    trim()
    addslashes()

    I tested these to make sure, and they do not affect that tag.

    Somewhere before you send the data to the database, something may be parsing out html. Is this a specific blogging package or your own code?
     
  3. madmatt

    madmatt Bow Down to the King Political User

    Messages:
    13,312
    Location:
    New York
    I wrote the code from sratch. It shows in the MySQL db/table correctly. However, when it is displayed as HTML it won't put the line break in (although the second <BR> is there).
     
  4. bush dogg

    bush dogg OSNN Senior Addict Political User

    Messages:
    433
    Location:
    Kansas
    I don’t recall using <br /> at the end to skip a line.

    I use <br> to skip a line. I would try a line with just <br> and see if that’s what you want.
     
  5. madmatt

    madmatt Bow Down to the King Political User

    Messages:
    13,312
    Location:
    New York
    nl2br adds the breaks in as <br /> automatically (XHTML compliant).
     
  6. SPeedY_B

    SPeedY_B I may actually be insane.

    Messages:
    15,800
    Location:
    Midlands, England
    <br /> = XHTML
    <br> = HTML

    They should both work, unless your browser decides to throw a fit, though technically they shouldn't be used together.
     
  7. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    personally I would not nl2br the string when you are putting it into the database. Then when you edit it you are doing to have a load of <br/> tags in your textarea. It would be better to maintain the newline (\n) characters and nl2br the text before you output it for the normal rendering.
     
  8. madmatt

    madmatt Bow Down to the King Political User

    Messages:
    13,312
    Location:
    New York
    I am using striptags (except <b><u><i><a>) when I bring it back into the textarea. How would I automatically add in the \n when I send it from the textarea to the MySQL db/table?
     
  9. SPeedY_B

    SPeedY_B I may actually be insane.

    Messages:
    15,800
    Location:
    Midlands, England
    That should be done automatically. I don't touch the line-breaks sent from my PHP to the SQL until it's read back and displayed on the page.
     
  10. madmatt

    madmatt Bow Down to the King Political User

    Messages:
    13,312
    Location:
    New York
  11. albybum

    albybum Penguin Rancher

    Messages:
    280
    Location:
    Elizabethton, TN
    nl2br should not be stripping out any html, much less line breaks. It's only purpose is to find and replace \n with <br />.

    I tested this to make sure.

    Check to make sure <br /> is excluded from striptags and you aren't stripping out html tags or < > symbols before you print your database or rss values.

    That's about the only thing I can imagine would cause the problem.

    [edit] View Source on your rss feeds show line breaks written as &lt;br /&gt; not <br />

    If they come back into the html that way, they might be interpreted as literals and not html tags.
     
  12. LordOfLA

    LordOfLA Godlike!

    Messages:
    7,027
    Location:
    Maidenhead, Berkshire, UK
    Edit, nevermind, scrolled along the text area...
     
  13. madmatt

    madmatt Bow Down to the King Political User

    Messages:
    13,312
    Location:
    New York
    The blog pulls the data right from the MySQL table. The RSS feed pulls in the data from the MySQL table then converts it to XML.
     
  14. madmatt

    madmatt Bow Down to the King Political User

    Messages:
    13,312
    Location:
    New York
    Never mind. I found a work around. I think it's a problem with IE and the way it handles <br> on an empty line.

    Code:
    $article = str_replace("<br />", "&nbsp;<br>", $article);
    
    Now the line isn't technically empty and contains a space. It works fine now. Thank you to everyone who helped.