Jump to content

nl2br(); Help needed.


DanyalSakrani

Recommended Posts

Hello,

 

My name is Danyal, and I am having problems with a small problem: My problem is that even though I added the "nl2br();" function, it still won't let me auto break a line when something is submitted into the database. Here is an example of what my problem looks like:

 

problemdx2.png

 

 

As you can see above, everytime a user (or myself, in this case), breaks a line, the following appears "rn". Here is my code:

 

<?php
include('Removed);
if(isset($_SESSION['user']))
{
$newsID = $_GET['newsID'];
$GetNews="SELECT * from News where ID='$newsID' LIMIT 1";
$GetNews2=mysql_query($GetNews) or die(mysql_error());
$GetNews3=mysql_fetch_array($GetNews2);
if(!$GetNews3)
{
print "This news story is not in our database!";
}
else
{
if(isset($_POST['Submit']))
{
$Comment  = $_POST['Comment'];
if(strlen($Comment) > '4000')
{
print "Your comment is too big! Please shorten it, and try again!";
}
else
{
if(strlen($Comment) < '2')
{
print "Your comment is too small! Please add more, and try again!";
}
else
{
$Current=date("U");
if($FetchTrainer3[LCommentTime] > $Current-10)
{
print "You may only post a comment every 10 seconds!";
}
else
{
$Comment[Comment] = ereg_replace("\r\n", "<br />", $Comment[Comment]);
$Comment = nl2br($Comment);
$Comment = htmlspecialchars($Comment);
$Date    = date("l \\T\h\e jS");
$Today   = date("U");
$query = mysql_query("Insert into Comments (newsID,Username,Comment,Time) values ('$newsID','$FetchTrainer3[username]','$Comment','$Date')") or die(mysql_error());
$CommentTime="UPDATE Trainers set LCommentTime='$Today' where Username='$GetTrainer3[username]' LIMIT 1";
mysql_query($CommentTime) or die(mysql_error());
}
}
}
}
if (!isset($_POST['Submit']))
{

print "				<h1>Comments</h1>";
$GetComments = mysql_query("SELECT * FROM Comments WHERE newsID = '$newsID' ORDER BY ID DESC LIMIT 16");
while($Comment = mysql_fetch_array($GetComments))
{
$Comment[Comment] = ereg_replace("\r\n\rn", "<br />", $Comment[Comment]);
$Comment[Comment] = nl2br($Comment[Comment]);
print "				<table width=\"100%\">
				<tr>
					<td valign=\"top\" width=\"30%\"><strong>Name</strong>: $Comment[username]</td>
                                                <td valign=\"top\" width=\"30%\"><strong>Date</strong>: $Comment[Time]</td>
                                                <td valign=\"top\" width=\"30%\"><strong>Comment</strong>: $Comment[Comment]</td>";
if($FetchTrainer3[status] > '0')
{
print "                                         <td valign=\"top\" width=\"30%\"><strong>Action</strong>: <a href=\"index.php?mode=deletecomment&commentID=$FetchComment3[iD]\">Delete comment</a>.</td>";
}
print "                                 </tr>
                                </table>";
}
print "				<h1>Submit A Comment</h1>
                                <form method=\"post\" action=\"index.php?mode=comments&newsID=$newsID\">
                                <p>
                                <table width=\"100\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">
			<tr align=\"center\">
			  <td colspan=\"4\"><strong>Emoticons</strong></td>

			</tr>
							<tr align=\"center\" valign=\"middle\"> 
			  				  <td><a href=\"javascript: addSmiley('')\"><img src=\"images/Emoticons/Biggrin.gif\" border=\"0\" alt=\"Very Happy\" title=\"Very Happy\" /></a></td>
			  				  <td><a href=\"javascript: addSmiley('')\"><img src=\"images/Emoticons/Smile.gif\" border=\"0\" alt=\"Smile\" title=\"Smile\" /></a></td>
			  				  <td><a href=\"javascript: addSmiley('')\"><img src=\"images/Emoticons/Sad.gif\" border=\"0\" alt=\"Sad\" title=\"Sad\" /></a></td>
			  				  <td><a href=\"javascript: addSmiley('')\"><img src=\"images/Emoticons/Surprised.gif\" border=\"0\" alt=\"Surprised\" title=\"Surprised\" /></a></td>
			  				</tr>
							<tr align=\"center\" valign=\"middle\"> 
			  				  <td><a href=\"javascript: addSmiley(':shock:')\"><img src=\"images/Emoticons/Eek.gif\" border=\"0\" alt=\"Shocked\" title=\"Shocked\" /></a></td>
			  				  <td><a href=\"javascript: addSmiley(':?')\"><img src=\"images/Emoticons/Confused.gif\" border=\"0\" alt=\"Confused\" title=\"Confused\" /></a></td>

			  				  <td><a href=\"javascript: addSmiley('')\"><img src=\"images/Emoticons/Cool.gif\" border=\"0\" alt=\"Cool\" title=\"Cool\" /></a></td>
			  				  <td><a href=\"javascript: addSmiley('')\"><img src=\"images/Emoticons/Lol.gif\" border=\"0\" alt=\"Laughing\" title=\"Laughing\" /></a></td>
			  				</tr>
							<tr align=\"center\" valign=\"middle\"> 
			  				  <td><a href=\"javascript: addSmiley(':x')\"><img src=\"images/Emoticons/Mad.gif\" border=\"0\" alt=\"Mad\" title=\"Mad\" /></a></td>
			  				  <td><a href=\"javascript: addSmiley('')\"><img src=\"images/Emoticons/Radd.gif\" border=\"0\" alt=\"Radd\" title=\"Radd\" /></a></td>
			  				  <td><a href=\"javascript: addSmiley(':opps:')\"><img src=\"images/Emoticons/Redface.gif\" border=\"0\" alt=\"Embarassed\" title=\"Embarassed\" /></a></td>
			  				  <td><a href=\"javascript: addSmiley('')\"><img src=\"images/Emoticons/Cry.gif\" border=\"0\" alt=\"Crying or Very sad\" title=\"Crying or Very sad\" /></a></td>
			  				</tr>

							<tr align=\"center\" valign=\"middle\"> 
			  				  <td><a href=\"javascript: addSmiley(':evil:')\"><img src=\"images/Emoticons/Evil.gif\" border=\"0\" alt=\"Evil or Very Mad\" title=\"Evil or Very Mad\" /></a></td>
			  				  <td><a href=\"javascript: addSmiley(':twisted:')\"><img src=\"images/Emoticons/Twisted.gif\" border=\"0\" alt=\"Twisted Evil\" title=\"Twisted Evil\" /></a></td>
			  				  <td><a href=\"javascript: addSmiley(':roll:')\"><img src=\"images/Emoticons/Rolleyes.gif\" border=\"0\" alt=\"Rolling Eyes\" title=\"Rolling Eyes\" /></a></td>
			  				  <td><a href=\"javascript: addSmiley('')\"><img src=\"images/Emoticons/Wink.gif\" border=\"0\" alt=\"Wink\" title=\"Wink\" /></a></td>
			  				</tr>
							<tr align=\"center\" valign=\"middle\"> 
			  				  <td><a href=\"javascript: addSmiley(':!:')\"><img src=\"images/Emoticons/Exclaim.gif\" border=\"0\" alt=\"Exclamation\" title=\"Exclamation\" /></a></td>
			  				  <td><a href=\"javascript: addSmiley(':?:')\"><img src=\"images/Emoticons/Question.gif\" border=\"0\" alt=\"Question\" title=\"Question\" /></a></td>
			  				  <td><a href=\"javascript: addSmiley('')\"><img src=\"images/Emoticons/Idea.gif\" border=\"0\" alt=\"Idea\" title=\"Idea\" /></a></td>

			  				  <td><a href=\"javascript: addSmiley(':arrow:')\"><img src=\"images/Emoticons/Arrow.gif\" border=\"0\" alt=\"Arrow\" title=\"Arrow\" /></a></td>
			  				</tr>
                                </table>
			<label>Your Comments</label>
                                <textarea rows=\"5\" cols=\"5\" name=\"Comment\" placeholder=\"Write yours comments...\" onselect=\"storeCaret(this);\" onclick=\"storeCaret(this);\" onkeyup=\"storeCaret(this);\"></textarea>
                                <br />
                                <input type=\"Submit\" value=\"Submit\" class=\"button\" name=\"Submit\" />
                                </p>
                                </form>
                                <br />";
}
}
}
if(!isset($_SESSION['user']))
{
print "You need to be logged in to view this page!";
}
?>

 

Can anyone help me? Thank you,

 

Danyal

Link to comment
https://forums.phpfreaks.com/topic/57141-nl2br-help-needed/
Share on other sites

Nope, I submitted a new comment, and yet, still get this:

 

Comment: TESTrnrnTEST

 

I did this:

 

$GetComments = mysql_query("SELECT * FROM Comments WHERE newsID = '$newsID' ORDER BY ID DESC LIMIT 16");
while($Comment = mysql_fetch_array($GetComments))
{
$Comment[Comment] = str_replace("\r\n", "<br />", $Comment[Comment]);

Link to comment
https://forums.phpfreaks.com/topic/57141-nl2br-help-needed/#findComment-282341
Share on other sites

it worked for me when I tried it.

 

$text = "I\r\nRule";

$text = str_replace("\r\n", "<br />" , $text);

echo $text;

 

Yeah, but mine doesn't show \r, it shows both "rn". Not "\r\n".

 

Not sure, it could be the way the server is setup to retrieve the data. You can use ereg_replace to create a regex that will replace only rn but if someone enters turn well yea. I would check on your server settings and make sure that something is not skewed there.

 

Ace is talking about www.php.net/stripslashes.

Link to comment
https://forums.phpfreaks.com/topic/57141-nl2br-help-needed/#findComment-282354
Share on other sites

I tried making a function that does both, no succes

 

<?php

function format_text ($text){

	$text = nl2br ($text);
	$text = str_replace ('\n', '<br />', $text);
	return $text;

}

format_text ($comment['Comment']);

?>

 

I tried this, and it worked:

 

$C = $comment[Comment];
$CS1 = str_replace ('rn', '<br />', $C);

 

But now it replaces ALL RN's! Meaning, if someone writes "learn" it will show "lea". I tried the /r/n, but nothing. Do I contact my server?

Link to comment
https://forums.phpfreaks.com/topic/57141-nl2br-help-needed/#findComment-283271
Share on other sites

What does the data look like when you receive it before it goes into the DB?

 

Do a print_r($_POST)  and paste that here (you may have to view the page source so we can see the new lines etc). Have you tried nl2br before inputting it into the DB? I know its looked down upon but it may be what you need.

Link to comment
https://forums.phpfreaks.com/topic/57141-nl2br-help-needed/#findComment-283469
Share on other sites

i suggest you clear you comment of anything that is going in the database just simple put

 

$text=$_post['message'];

 

and insert it into the database.. once you confirm that the nl2br works with out putting the mesage you can add everything else..

because i know thats the problem.

Link to comment
https://forums.phpfreaks.com/topic/57141-nl2br-help-needed/#findComment-283473
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.