Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

Eliminating <BR> from textarea

I've used a form to input some text into a mysql database where I qualifed what was being inputted using n2lbr to ensure accuracy where the line breaks were concerned.

Problem is, when I go to edit that text in a textarea field, I see this:

Oh man, this is sweet!<br />
<br />
Marj will have no problem typing this in!

I've got to get rid of the <br/> characters as that will completely freak my user out. How can I do it?
Avatar of 0791882310
0791882310

replace all <br> or whatever with \n in the programming logic before it's inserted into the textarea... like

$finalText = str_replace("<br>" , "\n");
Avatar of Beverley Portlock
Replace the <br/> with \r\n using str_replace

$textAreaString = str_replace( array("<br>", "<br/>"), "\r\n", $databaseString );
Just noticed that your BRs have a space in them so I have amended the code to include that style as well as the other two. I've added your code so you can see how it is intended to work.

<?php
$databaseString = "Oh man, this is sweet!<br />
<br />
Marj will have no problem typing this in!
";
$textAreaString = str_replace( array("<br>", "<br/>", "<br />"), "\r\n", $databaseString );
echo "<textarea>$textAreaString</textarea>";
?>
Actually you should use the nl2br function when you retrieve information from the database, insert directly from the textarea (after using mysql_real_escape_string() function to escape quote ' and double quote " ).
Avatar of Bruce Gust

ASKER

OK, here's what I've got:
<tr>
<td colspan="2">&nbsp;<BR>
<textarea name="summary"><?php echo stripslashes($summary); ?></textarea>
</td>
</tr>

The textarea code that I have above produces this in the textarea itself:

I've had some experience doing this.<br />
<br />
But I've not always succeeded...

In the next textarea on my page, I went ahead and tried bportlock's suggestion, which looked like this:
<tr>
<td colspan="2">&nbsp;<BR>
<textarea name="body">
<?php $textAreaString = str_replace( array("<br>", "<br/>"), "\r\n", $body );
echo $textAreaString;
?>
</textarea>

...and here's what I got in the actual textarea:

(this first line was indented almost half the page                  Oh man, this is sweet!<br />
<br />
Marj will have no problem typing this in!
</td>
</tr>

So where am I blowing it?
really??.. you left the space out in the <br/> tag... <br />
Indeed. I modded this

$textAreaString = str_replace( array("<br>", "<br/>"), "\r\n", $body );

Like this

$textAreaString = str_replace( array("<br>", "<br/>", "<br />"), "\r\n", $body );

it's a few comments up.
I want to make sure I'm explaining this right, because it may very well be that my problem is the way I'm inputting my text.

Here's what I'm putting in my textarea on the form that's going to input it into my database:

Man, this is sweet!

I can't wait to see what this looks like!

Now, here's the way that I'm "preparing" that text for insertion into my table:

$body = mysqli_real_escape_string($cxn, trim(nl2br($_POST['body'])));

NOW, the problem is when I go to display the text that I've inserted into the database using the method above.

The way that's it's coming from the database to my textarea looks like this:

Man, this is sweet</br>
</br>
I can't wait to see what this looks like.

How do I get rid of the </br> tags so my non-techy users don't see that and freak out?

After reading some of the posts, I was thinking that maybe y'all are thinking I'm having trouble inputting the text. I'm not. It's displaying what I've inputted in a text field after the fact so I can edit it.

ASKER CERTIFIED SOLUTION
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial