robert_gsfame Posted May 7, 2010 Share Posted May 7, 2010 I got this question in my mind.....when should we use nl2br? is it used during inserting the data?? like $data=nl2br($_POST['text']); So then i will have all html code stored inside the database like <br><br> but what if let say i type "my name is <br> John" in the textbox will this <br>changed into newline when retrieving the data from database?? Link to comment https://forums.phpfreaks.com/topic/201004-nl2br-small-problem/ Share on other sites More sharing options...
cs.punk Posted May 7, 2010 Share Posted May 7, 2010 I got this question in my mind.....when should we use nl2br? is it used during inserting the data?? like $data=nl2br($_POST['text']); So then i will have all html code stored inside the database like <br><br> but what if let say i type "my name is <br> John" in the textbox will this <br>changed into newline when retrieving the data from database?? Yes it would become a linebreak literally. Unless you escape it (with htmlentities) or strip it etc. Link to comment https://forums.phpfreaks.com/topic/201004-nl2br-small-problem/#findComment-1054586 Share on other sites More sharing options...
kenrbnsn Posted May 7, 2010 Share Posted May 7, 2010 The nl2br() function should only be used when displaying text that might contain the newline character on the screen. The newline character is NOT the html tag "<br \>", but the character(s) represented by "\n", "\r", or "\r\n" depending on the OS your script runs on. The function doesn't replace the newline with "<br \>" but adds the "<br \> before the newline in the string. Ken Link to comment https://forums.phpfreaks.com/topic/201004-nl2br-small-problem/#findComment-1054591 Share on other sites More sharing options...
trq Posted May 7, 2010 Share Posted May 7, 2010 Yes it would become a linebreak literally. No it wouldn't. it would be untouched. Anyway, to the op. Its generally best practice to store your data in the format it was received in (once being validated of course). You can then format the output in a choice of different ways depending on the application. Link to comment https://forums.phpfreaks.com/topic/201004-nl2br-small-problem/#findComment-1054592 Share on other sites More sharing options...
robert_gsfame Posted May 7, 2010 Author Share Posted May 7, 2010 if i use htmlentities then i will get another problem as all newline <br/> that user has from ENTER button also shown as <br/> not newline So then i cannot prevent this happen? Link to comment https://forums.phpfreaks.com/topic/201004-nl2br-small-problem/#findComment-1054593 Share on other sites More sharing options...
robert_gsfame Posted May 7, 2010 Author Share Posted May 7, 2010 okay i got the answer now...when display the record then i have to combine nl2br(htmlentities()) so that whatever html tag will be shown thx! Link to comment https://forums.phpfreaks.com/topic/201004-nl2br-small-problem/#findComment-1054598 Share on other sites More sharing options...
cs.punk Posted May 7, 2010 Share Posted May 7, 2010 Yes it would become a linebreak literally. No it wouldn't. it would be untouched. Mind to explain? <?php echo nl2br("Bob <br /> Bob2 <br />"); ?> // Source Bob <br /> Bob2 <br /> Link to comment https://forums.phpfreaks.com/topic/201004-nl2br-small-problem/#findComment-1054619 Share on other sites More sharing options...
trq Posted May 7, 2010 Share Posted May 7, 2010 Yes it would become a linebreak literally. No it wouldn't. it would be untouched. Mind to explain? <?php echo nl2br("Bob <br /> Bob2 <br />"); ?> // Source Bob <br /> Bob2 <br /> A <br /> tag is NOT a line break. Link to comment https://forums.phpfreaks.com/topic/201004-nl2br-small-problem/#findComment-1054874 Share on other sites More sharing options...
cs.punk Posted May 9, 2010 Share Posted May 9, 2010 Yes it would become a linebreak literally. No it wouldn't. it would be untouched. Mind to explain? <?php echo nl2br("Bob <br /> Bob2 <br />"); ?> // Source Bob <br /> Bob2 <br /> A <br /> tag is NOT a line break. Ok its an HTML line break? Thats what he was asking if you ask me.. Link to comment https://forums.phpfreaks.com/topic/201004-nl2br-small-problem/#findComment-1055493 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.