Jump to content

nl2br not working?


angel1987

Recommended Posts

I am using textarea to get data and entering it into database using addslashes and mysql_real_escape_string

 

$text = $_POST['txttext'];
$text = addslashes($text);
$text = mysql_real_escape_string($text);

 

Then displaying the data from database on other page using stripslashes and htmlentities

 

$text = stripslashes(htmlentities($rows['text']));
echo nl2br($text);

 

Entire text stretches across the screen and shows in 1 line. Is it because use of addslashes, stripslashes etc ?

Link to comment
Share on other sites

you could use wordwrap() if all else fails...

 

Looks to me like you are overdoing the slashes, mysql_real_escape_string() does exactly the same thing as addslashes() you you might want to consider revising your method.  Try stripping it down to raw data, and then progressing from there - but mysql_real_escape_string() is the preferred method of making the user generated data safer for injection into your DB.

 

Also you have htmlentities on the wrong side, you use that to make the HTML data database safe, you dont really need to use it for preparation for use in the textarea.

 

Have a play though, see what fits, but I find wordwrap() to be quite useful.

 

Cheers,

Rw

Link to comment
Share on other sites

I am using textarea to get data and entering it into database using addslashes and mysql_real_escape_string

 

$text = $_POST['txttext'];
$text = addslashes($text);
$text = mysql_real_escape_string($text);

 

Then displaying the data from database on other page using stripslashes and htmlentities

 

$text = stripslashes(htmlentities($rows['text']));
echo nl2br($text);

 

Entire text stretches across the screen and shows in 1 line. Is it because use of addslashes, stripslashes etc ?

 

 

$text = $_POST['txttext'];
$text = nl2br($text);
$text = mysql_real_escape_string($text);

That is all that is necessary. nl2br() needs to be done before you place it in the DB. then you can just echo the text.

Link to comment
Share on other sites

I am using textarea to get data and entering it into database using addslashes and mysql_real_escape_string

 

$text = $_POST['txttext'];
$text = addslashes($text);
$text = mysql_real_escape_string($text);

 

Then displaying the data from database on other page using stripslashes and htmlentities

 

$text = stripslashes(htmlentities($rows['text']));
echo nl2br($text);

 

Entire text stretches across the screen and shows in 1 line. Is it because use of addslashes, stripslashes etc ?

 

 

$text = $_POST['txttext'];
$text = nl2br($text);
$text = mysql_real_escape_string($text);

That is all that is necessary. nl2br() needs to be done before you place it in the DB. then you can just echo the text.

No, it shouldn't! It is best to call nl2br when you are displaying the data from the database.

 

I think the issue is with addslashes/stripslashes. There is no need to call addslashes if you're using mysql_real_escape_string. You can also remove the call to strip_slashes here

$text = stripslashes(htmlentities($rows['text']));

This is most probably affecting the newlines within your text and thus nl2br doesn't work.

Link to comment
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.