Jump to content

TextArea and nl2br


doubledee

Recommended Posts

Please help refresh my memory...

 

It seems to me that there is some combination with nl2br that is a no-no...

 

I am thinking I need to remove nl2br in this code...

<textarea id="comments" name="comments" cols="50" rows="15"><?php if (isset($comments)){echo nl2br(htmlentities($comments, ENT_QUOTES));} ?></textarea>

 

:shrug:

 

 

Debbie

 

Link to comment
Share on other sites

The function nl2br() is used to insert new html line breaks in front of new line in a string. In your case you should use this function because if user enter their comments in one more lines it will be displayed together if you don't use the function.

else your user must define the line breaks in their comment itself.

 

The function htmlentities() convert the string into HTML entities so if you use htmlentities() function with the nl2br() function it will display the line break code (<br/>) with the output

 

then your code will be

 


<textarea id="comments" name="comments" cols="50" rows="15"><?php if (isset($comments)){echo nl2br($comments, ENT_QUOTES);} ?></textarea>

Link to comment
Share on other sites

Textareas contain text, not HTML. They also preserve whitespace and newlines. If you use nl2br() then it will turn the perfectly legitimate newlines into HTML tags which will then be displayed literally.

 

So yes, remove it. But HTML entities are fine so keep that one.

 

[edit] SaCH is right for the general case, but textareas are special.

Link to comment
Share on other sites

Textareas contain text, not HTML. They also preserve whitespace and newlines. If you use nl2br() then it will turn the perfectly legitimate newlines into HTML tags which will then be displayed literally.

 

So yes, remove it. But HTML entities are fine so keep that one.

 

[edit] SaCH is right for the general case, but textareas are special.

 

So what is the purpose of keeping the htmlentites() with it ? can you just clear it ?

Link to comment
Share on other sites

Textareas contain text, not HTML. They also preserve whitespace and newlines. If you use nl2br() then it will turn the perfectly legitimate newlines into HTML tags which will then be displayed literally.

 

So yes, remove it. But HTML entities are fine so keep that one.

 

[edit] SaCH is right for the general case, but textareas are special.

 

Thanks!!!

 

 

Debbie

 

Link to comment
Share on other sites

So what is the purpose of keeping the htmlentites() with it ? can you just clear it ?

"Clear it"? htmlentities() is about getting characters represented correctly on the page. As in if the page doesn't have the right text encoding to support something you can use entities to get the character to show up anyways. It'll also escape HTML tags but that's a good side effect.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.