Solved

Eliminating <BR> from textarea

Posted on 2009-05-16
10
380 Views
Last Modified: 2013-12-12
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?
0
Comment
Question by:brucegust
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 6

Expert Comment

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

$finalText = str_replace("<br>" , "\n");
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24404161
Replace the <br/> with \r\n using str_replace

$textAreaString = str_replace( array("<br>", "<br/>"), "\r\n", $databaseString );
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24404187
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>";
?>
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Expert Comment

by:gamebits
ID: 24404195
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 " ).
0
 

Author Comment

by:brucegust
ID: 24404199
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?
0
 
LVL 6

Expert Comment

by:0791882310
ID: 24404221
really??.. you left the space out in the <br/> tag... <br />
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24404230
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.
0
 

Author Comment

by:brucegust
ID: 24404244
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.

0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 250 total points
ID: 24404275
In the examples you gave above, your first one was this


<tr>
<td colspan="2">&nbsp;<BR>
<textarea name="summary"><?php echo stripslashes($summary); ?></textarea>
</td>
</tr>

and in the one where you inserted my code you had this

<tr>
<td colspan="2">&nbsp;<BR>
<textarea name="body">
<?php $textAreaString = str_replace( array("<br>", "<br/>", "<br />"), "\r\n", $body );
echo $textAreaString;
?>
</textarea>

In the first example you have $summary, but in the second example you used $body. Why did it change? When you retrieve the data from the database it obviously goes in a variable. It is that variable that you need to feed into str_replace.

Combining your first example with the second I would expect the following code to work


<tr>
<td colspan="2">&nbsp;<BR>
<textarea name="body">
<?php $textAreaString = str_replace( array("<br>", "<br/>", "<br />"), "\r\n", $summary );
echo  $textAreaString;
?>
</textarea>

0
 
LVL 28

Assisted Solution

by:gamebits
gamebits earned 250 total points
ID: 24404282
Like I said do not use nl2br to input text in the database and see what happen.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question