doubledee Posted March 18, 2012 Share Posted March 18, 2012 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> Debbie Quote Link to comment https://forums.phpfreaks.com/topic/259187-textarea-and-nl2br/ Share on other sites More sharing options...
SaCH Posted March 18, 2012 Share Posted March 18, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/259187-textarea-and-nl2br/#findComment-1328722 Share on other sites More sharing options...
requinix Posted March 18, 2012 Share Posted March 18, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/259187-textarea-and-nl2br/#findComment-1328725 Share on other sites More sharing options...
SaCH Posted March 18, 2012 Share Posted March 18, 2012 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/259187-textarea-and-nl2br/#findComment-1328796 Share on other sites More sharing options...
doubledee Posted March 18, 2012 Author Share Posted March 18, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/259187-textarea-and-nl2br/#findComment-1328814 Share on other sites More sharing options...
requinix Posted March 18, 2012 Share Posted March 18, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/259187-textarea-and-nl2br/#findComment-1328874 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.