0

I am having a problem with some textarea things. I insert text into my database using a textarea like

<textarea id="e1" name="content"></textarea>

My php file then runs some stuff on it as shown to make HTML tags out of \n and \r:

$str     = $_POST['content'];
$order   = array("\r\n", "\n", "\r");
$replace = '<br />';
$content = mysql_real_escape_string(str_replace($order, $replace, $str));

and the result gets inserted into a database. I then call the result from the database in $content and put that in a text area like so:

<textarea id="e1" name="content"><?php echo $content ?></textarea>

When I view that page the <br /> tags are visible.
An Example:
I submit this into the text area

Hello,
This is text.
Best Rergards,
Testificus

It gets processed by my php code and then is echoed into the textarea. When it is echoed into the textarea it comes out in the form:

Hello,<br />This is text.<br />Best Regards,<br />Testificus

Is there any way of having the text look like before with the <br /> tags in action rather that being presented as text? Thanks for your help and let me know if it is not clear what I mean.

4
  • try using <?php echo strip_tags($content) ?> Commented Dec 8, 2011 at 6:29
  • Remove str_replace($order, $replace, $str). mysql_real_escape_string is enough. Commented Dec 8, 2011 at 6:30
  • @dfsq The str_replace is to convert the /n tags to <br /> tags (as the text gets emailed out in html form later) Commented Dec 8, 2011 at 6:34
  • @Dinesh That seems to just take out the tags and leave everything on one line still. Commented Dec 8, 2011 at 6:35

4 Answers 4

1

You can leave the \r\n's in the database and then when you want to output to html, simply run the text through nl2br() which swaps new lines for <br> elements. If you do it this way you don't need to worry about extra converting, it is done only once when you need it.

Sign up to request clarification or add additional context in comments.

Comments

0

Am I right, you want to replace the newlines with the <br /> tag? Why don't you just use nl2br().

If you want to keep using your example, you have to use:

$replace = "<br />\r\n";

Comments

0

I'm not sure how valid it is but

&#10; 

in place of the newlines should work.

2 Comments

Out of interest, how would I do that and what does it do?
change it to $replace = '&#10;';
0

Replace your code

$str     = $_POST['content'];
$order   = array("\r\n", "\n", "\r");
$replace = '<br />';
$content = mysql_real_escape_string(str_replace($order, $replace, $str));

with this code :

$str     = $_POST['content'];
$content = mysql_real_escape_string(nl2br($str));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.