Jump to content

Using nl2br to add line breaks in textarea field


trevb

Recommended Posts

Hi all,

 

I would be very appreciative if I could solicit someone's help as it relates to the use of nl2br. Essentially, I'm trying to maintain line breaks when a user adds information to a textarea field. Now, this is fairly straight forward, but I've run into a few problems I'm sure someone here could fix up pretty quickly.

 

Below is how I;m currently using nl2br:

 

$row->custom1 = ampReplace( nl2br($row->custom1) );

$row->custom2 = ampReplace( nl2br($row->custom2) );

$row->custom3 = ampReplace( nl2br($row->custom3) );

 

Now this works ok, as it adds the required line breaks to the field. The problem occurs when a user makes additional changes to the form and resubmits it. Now it adds an additional line break to the form, so it now appears with two carriage returns. If he/she re-edits it, there would be three <br /> tags.

 

So my question is twofold:

 

1. How can I use nl2br (or some other method) to maintain line break within a text area field without displaying the <br /> tag when the form is re-edited?

2. How can I avoid multiple line breaks when the form is edited and re-submitted?

 

I apologize if this has been touched on before. I begin all posts by first searching previous posts, but I either did not find what I was looking for, or failed to understand what was being explained.

 

Thank you all in advance!

 

Trev B

the trick is dual storage (the same holds true with BB tags)  what you do is you carry across the raw data (in your case the $_POST data) as a variable apply your basic sanatizing to it except for nl2br then store a formatted version with nl2br and then you can have both i.e

<?php
$var = $_POST['textarea'];
$var_1 = mysql_real_escape_string(strip_tags($var);
$var_2 = nl2br($var1);
//now you want to re  echo that text area so you do
echo "<textarea>".$var_1."</textarea>";
//But you want to show the formatted output farther down
echo "Formatted stuff:".$var_2;
?>

Makes sense, its easier than trying to reverse it after you nl2br it

the trick is dual storage (the same holds true with BB tags)  what you do is you carry across the raw data (in your case the $_POST data) as a variable apply your basic sanatizing to it except for nl2br then store a formatted version with nl2br and then you can have both i.e

<?php
$var = $_POST['textarea'];
$var_1 = mysql_real_escape_string(strip_tags($var);
$var_2 = nl2br($var1);
//now you want to re  echo that text area so you do
echo "<textarea>".$var_1."</textarea>";
//But you want to show the formatted output farther down
echo "Formatted stuff:".$var_2;
?>

Makes sense, its easier than trying to reverse it after you nl2br it

 

Makes perfect sense. Let me give this a shot and if I have any problems I will let you know.

 

Thanks for taking the time to provide assistance!

 

Cheers!

 

Trev b

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.