Manixat Posted November 28, 2012 Share Posted November 28, 2012 (edited) Hello, Can someone explain to me how nl2br works because I cannot get it to work correctly I tried using it before escaping the input and after escaping the input and new lines are not shown in the database ? ( as <br> ) Here's some code if you'd like: $title = mysql_real_escape_string(stripslashes(nl2br($_POST["title"]))); $about = nl2br(mysql_real_escape_string(stripslashes($_POST["about"]))); Edited November 28, 2012 by Manixat Quote Link to comment https://forums.phpfreaks.com/topic/271318-nl2br/ Share on other sites More sharing options...
mrMarcus Posted November 28, 2012 Share Posted November 28, 2012 (edited) You really shouldn't use nl2br() on data being inserted into a database. Preserve the line-breaks on insert, and use nl2br on output. Edited November 28, 2012 by mrMarcus Quote Link to comment https://forums.phpfreaks.com/topic/271318-nl2br/#findComment-1396046 Share on other sites More sharing options...
Manixat Posted November 28, 2012 Author Share Posted November 28, 2012 (edited) Well I suppose, but it still doesn't work, no new lines on my output :/ <div class='info'>".nl2br(htmlspecialchars($row['info']))."</div> Edited November 28, 2012 by Manixat Quote Link to comment https://forums.phpfreaks.com/topic/271318-nl2br/#findComment-1396047 Share on other sites More sharing options...
mrMarcus Posted November 28, 2012 Share Posted November 28, 2012 Are the line breaks NOT being preserved in the database? If you do a simple, straight insert into the db from a textarea box with multiple carriage returns, they should be retained. Then, do a simple output of that record using only nl2br() to ensure that you're setup correctly. <div class='info'>". nl2br($row['info']) ."</div> Quote Link to comment https://forums.phpfreaks.com/topic/271318-nl2br/#findComment-1396049 Share on other sites More sharing options...
Manixat Posted November 28, 2012 Author Share Posted November 28, 2012 Oh I found out where the problem is, my submit form doesn't insert new lines into the database, if I add them manually they appear in the output as well. What might the problem be? I guess if stripslashes was removing the slash before \r\n I would still get "r" or "n" ? which I don't ? I'm really confused :/ Quote Link to comment https://forums.phpfreaks.com/topic/271318-nl2br/#findComment-1396050 Share on other sites More sharing options...
Christian F. Posted November 28, 2012 Share Posted November 28, 2012 Only use stripslashes () if magic_quotes are activated, something they never should be. Wrap it up in a test, and then you don't have to worry about it. Lots of examples on the net, should you need one. Also, run a var_dump () on the $_POST array, then you'll see if the form does indeed send any newlines to the PHP parser. Remember to view source, or echo <pre> before the var_dump () call. If it does, then you should only use htmlspecialchars () and nl2br () right before sending the output to the browser, as you've done in the third post. Quote Link to comment https://forums.phpfreaks.com/topic/271318-nl2br/#findComment-1396055 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.