Jump to content

Recommended Posts

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 by Manixat
Link to comment
https://forums.phpfreaks.com/topic/271318-nl2br/
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/271318-nl2br/#findComment-1396049
Share on other sites

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 :/

Link to comment
https://forums.phpfreaks.com/topic/271318-nl2br/#findComment-1396050
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/271318-nl2br/#findComment-1396055
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.