Jump to content

[SOLVED] nl2br help


Zergman

Recommended Posts

I read through some previous posts regarding nl2br and I just don't get it.

 

I did learn that its advised to use nl2br outputting the data, not inputting.  Formatting seems correct in the database for the entered text so it looks like its going in right.

 

I just can't figure out how to use nl2br.  Don't even know what code to post here lol.

Link to comment
Share on other sites

Long explanation:

 

nl2br() just changes all the line breaks (\n)

 

V line break

 

^ line break

 

Into <br /> html tags... So when, for instance, someone enters some text into a form, a good script would put that data as-is into a database (assuming it's secure). Then, later, a different script would query the database, and when displaying it, it would nl2br() the data so that the line breaks are displayed in the html. (line breaks don't show up on the rendered end of html without <br /> tags).

 

For example one script inserts a blog post

 

<?php
$title = $_POST['title'];
$body = $_POST['body'];

// escape the quotes here

mysql_query("INSERT INTO news (title, body) VALUES ('$title', '$body')");
?>

 

And another script gets the info out and uses nl2br()

 

<?php
$resource = mysql_query("SELECT * FROM news");

while($row = mysql_fetch_assoc($resource))
{
  $row['body'] = nl2br($row['body']);
  echo "TITLE: {$row['title']} <br />";
  echo "BODY: {$row['body']}";
}
?>

 

So a body post in the above script of

 

This is a test post.
Hi!

 

would become

 

This is a test post.<br />Hi!

Link to comment
Share on other sites

Good stuff!  I did check the php manual, but it went WAY over my head.  Didn't make too much sense to me  ???

 

So if I understand this right, when I echo the information I want formatted

Ex

<td><?php echo $row_rsexdisplay['notes']; ?></td>

 

And this is me taking a wild stab in the dark

<?php echo nl2br($row_rsexdisplay['notes']); ?>

 

Do i have it right?

Link to comment
Share on other sites

that is fairly straightforward just remeber that nl2br isn't what it says it is in the title

 

it sounds like New Line to html Break

 

However its really

 

Insert html Break before New Lines

 

So running

nl2br(nl2br($var));  will produce double breaklines since it doesn't destroy the \n or \r present

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.