SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Textarea is showing <br />

    How do I prevent/suppress the displaying of html tags such as <br /> in text areas? If someone hits the return key, the tag is displayed. Below is an example. This makes it very hard to read. Thanks.

    On 6/19/2014, please change full backup of RMAN_PROXY_RAC_FULL_CHAI_EUEBP/RMAN_PROXY_FULL_CHAIN_EUAPP<br />to incremantal<br />RMAN_PROXY_RAC_INCR_CHAI_EUEBP/<br />RMAN_PROXY_INCR_CHAIN_EUAPP<br /><br />On Friday, 6/20, Suspend chain ESG_DAILY_2100_EST.<br /><br />On 6/21, create temporary on demand chain of full backup of RMAN_PROXY_RAC_FULL_CHAI_EUEBP/RMAN_PROXY_FULL_CHAIN_EUAPP

  2. #2
    SitePoint Addict WolfShade's Avatar
    Join Date
    Mar 2014
    Location
    St. Louis, MO, USA
    Posts
    251
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Odd that the textarea inserts <br /> if the enter key is hit. Usually, it's when text is retrieved from a database that it happens. I've never seen the line break tag entered in a textarea like that.

    If that's true, then I think it would have to be coded to do that. Unless I'm missing something.

    V/r,

    ^_^

  3. #3
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for the confusion. This would be after the form has been submitted (when retrieving from the server).

  4. #4
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,259
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Somewhere your code must be running a nl2br function (or equivalent) on the text retrieved from the database. Which is what you want when displaying on the page, but not what you want when displaying in a textarea. Since we don't have access to your code, there isn't much advice we can give except to tell you to look for the spot in your code where newlines are converted to <br> tags, and avoid making that replacement when you want to render in a textarea.
    "First make it work. Then make it better."

  5. #5
    SitePoint Addict WolfShade's Avatar
    Join Date
    Mar 2014
    Location
    St. Louis, MO, USA
    Posts
    251
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ah.. if it's happening with retrieved data that is to be displayed in a browser page, as @Jeff Mott points out that's what you want. It's just a matter of running the data through some kind of filter to replace the <br /> with line breaks (which I think depends upon which OS you're using.) Then re-replacing the line breaks with <br /> when saving the data.

    What SS platform are you developing in?

    ^_^

  6. #6
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for the ignorance, but what is SS?

  7. #7
    SitePoint Addict WolfShade's Avatar
    Join Date
    Mar 2014
    Location
    St. Louis, MO, USA
    Posts
    251
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Server Side (ASP, JSP, PHP, ColdFusion, etc.)

    ^_^

  8. #8
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ooph...sorry about that. PHP...although I did not write that code myself. Someone who no longer works for the company wrote it and he was just learning it himself at the time. I am kind of doing the html/css part. Can you advise me about how to check for that lb2br code?

  9. #9
    SitePoint Addict WolfShade's Avatar
    Join Date
    Mar 2014
    Location
    St. Louis, MO, USA
    Posts
    251
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, IF the developer did, in fact, use nl2br, the documentation for it can be found here.

    While that is a PHP function, it's not the only way to do so. There's also str_replace(), which is a kind of manual way of doing it.

    I don't know of any PHP function that will do the opposite of nl2br(). But if str_replace() is being used before data is inserted into the database, then using str_replace() with the substrings reversed should put the line breaks back in to the textarea.

    ^_^

  10. #10
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well all I needed was another set of eyes. This was in the php code. I had all these forms working properly, but this was only in one of them.
    $File_List = ereg_replace("(\r\n|\n|\r)", "<br />", $File_List);

  11. #11
    SitePoint Addict WolfShade's Avatar
    Join Date
    Mar 2014
    Location
    St. Louis, MO, USA
    Posts
    251
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pwrmacbob View Post
    Well all I needed was another set of eyes. This was in the php code. I had all these forms working properly, but this was only in one of them.
    $File_List = ereg_replace("(\r\n|\n|\r)", "<br />", $File_List);
    Yup! That'll do it. RegEx replace line breaks with '<br />'.

    I don't know what to reverse it with in any language but ColdFusion: replace '/<br[^>]>/i' (can be '<br>', '<br/>', '<br />' or their upper-case equivalent) with #chr(13)&chr(10)# for a Windows line break. I'm sure PHP has its own code for line breaks, just don't know what it is.

    ^_^

  12. #12
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,259
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Although that's the quick fix, be aware it can make a long-term mess. I've been on projects where values get escaped then unescaped then escaped then unescaped at various points in the program flow. The best option is to escape just once, at the very latest moment, at the point where you actually need it. Ideally, your templates might look something like this:

    Code PHP:
    <h4>Your message:</h4>
    <p><?php echo nl2br(escapeHtml($message)) ?></p>
     
    <h4>Edit your message:</h4>
    <textarea><?php echo escapeHtml($message) ?></textarea>

    Notice that we're escaping HTML at the last possible moment, right as we use it in the template, and we're also converting newlines to <br>'s at the last possible moment. That gives us the flexibility to use nl2br in one place but not in the other.
    "First make it work. Then make it better."


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
  •