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?? Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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! Quote Link to comment 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 /> Quote Link to comment 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. Quote Link to comment 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.. Quote Link to comment 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.