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

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,

:slight_smile:

Sorry for the confusion. This would be after the form has been submitted (when retrieving from the server).

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.

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?

:slight_smile:

Sorry for the ignorance, but what is SS?

Server Side (ASP, JSP, PHP, ColdFusion, etc.)

:slight_smile:

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?

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.

:slight_smile:

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
|
|\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. :slight_smile:

:slight_smile:

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:

<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.