Jump to content

nl2br Help


DanyalSakrani

Recommended Posts

Hello All :),

 

I need help with one of my scripts (Reply.php for my Forums):

 

Everytime a user submits a post with a break line, this shows up, "rn".

 

An example:

 

// The user enters his post in a textbox:

 

"Hello, my name is Danyal.

 

I like cookies."

 

 

When I fetch the Post, this shows up:

 

"Hello, my name is Danyal.rnI like cookies."

 

 

Here is my INSERT code:

 

$Post = $_POST['Message'];

if($Post == "")

{

$Post = "[No Message]";

}

 

$Post=htmlspecialchars($Post);

$Post= nl2br($Post);

 

$Time = date("F j, Y, g:i a");

$query = mysql_query("Insert into Posts (Post,Poster,Time,TID,FID) values ('$Post','$getuser3[username]','$Time','$TID','$FID')") or die(mysql_error());

$query = mysql_query("SELECT * FROM Posts where Poster = '$getuser3[username]' order by ID desc limit 1") or die(mysql_error());

$row = mysql_fetch_array( $query );

$Today=date("U");

$UUT="update trainers set LPostTime='$Today' where Username='$getuser3[username]' LIMIT 1";

        mysql_query($UUT) or die(mysql_error());

$nFID = $row["TID"];

$pstamp = date("U")-0;

$query = mysql_query("update Threads set OS = '$pstamp' where ID = '$TID'") or die(mysql_error());

print "Reply Posted Sucessfully. You Are Now Being Redirected To The Topic.";

 

print "<meta http-equiv=refresh content='2; URL=index.php?function=ViewThread&TID=$nFID'>";

 

The nl2br doesn't work. Can anyone please fix the code for me? Thank you,

 

Danyal

Link to comment
Share on other sites

Try this:

 

<?php
$Post = $_POST['Message'];
if($Post == "")
{
$Post = "[No Message]";
}

$Post=htmlspecialchars(nl2br($Post));

$Time = date("F j, Y, g:i a");
$query = mysql_query("Insert into Posts (Post,Poster,Time,TID,FID) values ('$Post','$getuser3[username]','$Time','$TID','$FID')") or die(mysql_error());
$query = mysql_query("SELECT * FROM Posts where Poster = '$getuser3[username]' order by ID desc limit 1") or die(mysql_error());
$row = mysql_fetch_array( $query );
$Today=date("U");
$UUT="update trainers set LPostTime='$Today' where Username='$getuser3[username]' LIMIT 1";
         mysql_query($UUT) or die(mysql_error());
$nFID = $row["TID"];
$pstamp = date("U")-0;
$query = mysql_query("update Threads set OS = '$pstamp' where ID = '$TID'") or die(mysql_error());
print "Reply Posted Sucessfully. You Are Now Being Redirected To The Topic.";

print "<meta http-equiv=refresh content='2; URL=index.php?function=ViewThread&TID=$nFID'>";
?>

 

--FrosT

Link to comment
Share on other sites

Try this:

 

<?php
$Post = $_POST['Message'];
if($Post == "")
{
$Post = "[No Message]";
}

$Post=htmlspecialchars(nl2br($Post));

$Time = date("F j, Y, g:i a");
$query = mysql_query("Insert into Posts (Post,Poster,Time,TID,FID) values ('$Post','$getuser3[username]','$Time','$TID','$FID')") or die(mysql_error());
$query = mysql_query("SELECT * FROM Posts where Poster = '$getuser3[username]' order by ID desc limit 1") or die(mysql_error());
$row = mysql_fetch_array( $query );
$Today=date("U");
$UUT="update trainers set LPostTime='$Today' where Username='$getuser3[username]' LIMIT 1";
         mysql_query($UUT) or die(mysql_error());
$nFID = $row["TID"];
$pstamp = date("U")-0;
$query = mysql_query("update Threads set OS = '$pstamp' where ID = '$TID'") or die(mysql_error());
print "Reply Posted Sucessfully. You Are Now Being Redirected To The Topic.";

print "<meta http-equiv=refresh content='2; URL=index.php?function=ViewThread&TID=$nFID'>";
?>

 

--FrosT

 

Hey,

 

Nope :(.

 

I get this:

 

TestrnrnrnTest

Link to comment
Share on other sites

try it in plain format please cheers remember to press the enter button.

<?php
$Post = $_POST['Message'];
if($Post == "")
{
$Post = "[No Message]";
}

$Post=nl2br($Post);

$Time = date("F j, Y, g:i a");
$query = mysql_query("Insert into Posts (Post,Poster,Time,TID,FID) values ('$Post','$getuser3[username]','$Time','$TID','$FID')") or die(mysql_error());
$query = mysql_query("SELECT * FROM Posts where Poster = '$getuser3[username]' order by ID desc limit 1") or die(mysql_error());
$row = mysql_fetch_array( $query );
$Today=date("U");
$UUT="update trainers set LPostTime='$Today' where Username='$getuser3[username]' LIMIT 1";
         mysql_query($UUT) or die(mysql_error());
$nFID = $row["TID"];
$pstamp = date("U")-0;
$query = mysql_query("update Threads set OS = '$pstamp' where ID = '$TID'") or die(mysql_error());
print "Reply Posted Sucessfully. You Are Now Being Redirected To The Topic.";

print "<meta http-equiv=refresh content='2; URL=index.php?function=ViewThread&TID=$nFID'>";
?>

Link to comment
Share on other sites

try this please

<?php
$Post = $_POST['Message'];
if($Post == "")
{
$Post = "[No Message]";
}


$Post=mysql_real_esape_string(nl2br($Post));

$Time = date("F j, Y, g:i a");
$query = mysql_query("Insert into Posts (Post,Poster,Time,TID,FID) values ('$Post','$getuser3[username]','$Time','$TID','$FID')") or die(mysql_error());
$query = mysql_query("SELECT * FROM Posts where Poster = '$getuser3[username]' order by ID desc limit 1") or die(mysql_error());
$row = mysql_fetch_array( $query );
$Today=date("U");
$UUT="update trainers set LPostTime='$Today' where Username='$getuser3[username]' LIMIT 1";
        mysql_query($UUT) or die(mysql_error());
$nFID = $row["TID"];
$pstamp = date("U")-0;
$query = mysql_query("update Threads set OS = '$pstamp' where ID = '$TID'") or die(mysql_error());
print "Reply Posted Sucessfully. You Are Now Being Redirected To The Topic.";

print "<meta http-equiv=refresh content='2; URL=index.php?function=ViewThread&TID=$nFID'>";
?>

Link to comment
Share on other sites

<?php
$Post = $_POST['Message'];
if($Post == "")
{
$Post = "[No Message]";
}


$Post=mysql_real_escape_string(nl2br($Post));

$Time = date("F j, Y, g:i a");
$query = mysql_query("Insert into Posts (Post,Poster,Time,TID,FID) values ('$Post','$getuser3[username]','$Time','$TID','$FID')") or die(mysql_error());
$query = mysql_query("SELECT * FROM Posts where Poster = '$getuser3[username]' order by ID desc limit 1") or die(mysql_error());
$row = mysql_fetch_array( $query );
$Today=date("U");
$UUT="update trainers set LPostTime='$Today' where Username='$getuser3[username]' LIMIT 1";
         mysql_query($UUT) or die(mysql_error());
$nFID = $row["TID"];
$pstamp = date("U")-0;
$query = mysql_query("update Threads set OS = '$pstamp' where ID = '$TID'") or die(mysql_error());
print "Reply Posted Sucessfully. You Are Now Being Redirected To The Topic.";

print "<meta http-equiv=refresh content='2; URL=index.php?function=ViewThread&TID=$nFID'>";
?>

 

sorry edited

Link to comment
Share on other sites

I am not sure, it is worth a shot? How I see it is for some reason the nl2br is throwing off because it just does a newline, where as the post is returning a carriage return (\r) with a new line (\n)

 

So in theory, converting \r\n to <br/> should produce the desired results...in theory.

 

--FrosT

Link to comment
Share on other sites

<?
include('connect.php');
if(isset($_SESSION['user']))
{

$Current=date("U");
if($getuser3[LPostTime] > $Current-10)
{
  print "You Can Only Post Once Every 10 Seconds!";
}
else
{

if(isset($_POST['Save']))
{

if($type == "Locked" or $type == "PinLocked")
{
print "This Thread Is Locked!";


}
else
{
$TID = $_GET['TID'];
if($TID == "")
{
print "You Did Not Choose Which Thread To Post In!";


}
else
{
$query = mysql_query("SELECT * FROM Threads where ID = '$TID'") or die(mysql_error()); 
$row = mysql_fetch_array( $query );
$TID = $row["ID"]; 
$FID = $row["FID"]; 
$type = $row["Type"]; 
if($TID == "")
{
print "The Thread You Chose To Post In No Longer Exists!";


}
else
{

$Post = $_POST['Message'];
if($Post == "")
{
$Post = "[No Message]";
}


$Post = str_replace('\r\n', '<br />', $Post);

$Time = date("F j, Y, g:i a");
$query = mysql_query("Insert into Posts (Post,Poster,Time,TID,FID) values ('$Post','$getuser3[username]','$Time','$TID','$FID')") or die(mysql_error());
$query = mysql_query("SELECT * FROM Posts where Poster = '$getuser3[username]' order by ID desc limit 1") or die(mysql_error());
$row = mysql_fetch_array( $query );
$Today=date("U");
$UUT="update trainers set LPostTime='$Today' where Username='$getuser3[username]' LIMIT 1";
         mysql_query($UUT) or die(mysql_error());
$nFID = $row["TID"];
$pstamp = date("U")-0;
$query = mysql_query("update Threads set OS = '$pstamp' where ID = '$TID'") or die(mysql_error());
print "Reply Posted Sucessfully. You Are Now Being Redirected To The Topic.";

print "<meta http-equiv=refresh content='2; URL=index.php?function=ViewThread&TID=$nFID'>";


}
}
}
}
if(!isset($_POST['Save']))
{


$Quote = $_GET['Quote'];

if($Quote != "")
{
$query = mysql_query("SELECT * FROM Posts where ID = '$Quote'") or die(mysql_error()); 
$row = mysql_fetch_array( $query );
$post1 = $row["Post"];
$poster1 = $row["Poster"];
$time1 = $row["Time"];
$show_quote = "
[quote]".$post1."[/quote]";
}
print "<table width='50%'>
<tr>
<td class='top' width='50%'><center>Reply</center></td></tr></table>
<table width='50%' border='0' class='top'>
<form method='POST'>
<tr>
<td class='top'>Message:</td>
<td class='top'>

<textarea COLS='50' ROWS='5' accept-charset='iso-8859-1' name='Message' style='font: 10pt'Verdana'>$show_quote</textarea>
<br><center><button type='Submit' name='Save' value='Save' class='SearchB'>Post Reply</button></center></form>



</td></tr>
</table>";
}
}
}
if(!isset($_SESSION['user']))
{
print "<font face='verdana' size='1'>You Must Be You Must Be Logged In To Reply To A Topic!";
}
?>

 

Here is my code. I tried using str_replace with single quotes, it didn't work.

Link to comment
Share on other sites

It didn't work. When I edit the "rn" from my SQL, it works.

Of course it will as you have edited it.

 

However there is something that is escaping the newlines characters when you submit the form.

 

Create a new script and put this in it:

<?php

if(isset($_POST['msg']))
{
    $msg = $_POST['msg'];

    echo 'Raw data: <pre>' .htmlentities($msg) . "</pre>\n";

    echo "\n<hr />\n";

    echo "Processed data:<br />\n" . nl2br($msg);

    echo "\n<hr />\n";
}

?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  Message:<br />
  <textarea name="msg" rows="10" cols="50"></textarea><br />
  <input type="submit" name="submit" value="Post Message" />
</form>

what does that produce?

Link to comment
Share on other sites

OK so that works. There must be something in your code which escapes the newlines characters. Not sure what's up but I will have a look.

 

@ redarrow

like i said was your action='' and char-set

No that was my code DanyalSakrani said worked.

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.