Okay I have a textarea that edits a text div.
When the user edits the text and submits,
paragraphs and line breaks are passed to the div.

i.e (not the browser)text div after submit....

'WARNING - This email and any attachments may be private & confidential.

If received in error, please delete and inform us by return email.'

textarea after submit....


'WARNING - This email and any attachments may be private & confidential. If received in error, please delete and inform us by return email.'

So the question is how can I get those paragraphs and breaks stored
and retrieved from a Mysql database, when using strip tags?

snippet that makes html tags for new lines..

$text = $row['text'];
$headline = $row['headline'];

// Use \n for newline on all systems
$text = preg_replace("/(\r\n|\n|\r)/", "\n", $text);

// Only allow two newlines in a row.
$text = preg_replace("/\n\n+/", "\n\n", $text);

// Put <p>..</p> around paragraphs
$text = preg_replace('/\n?(.+?)(\n\n|\z)/s', "<p>$1</p>", $text);

// Convert newlines not preceded by </p> to a <br /> tag
$text = preg_replace('|(?<!</p>)\s*\n|', "<br />", $text);

snippet that echos database info and strips the html

<textarea rows="10" cols="50" name="text">
<?php
echo strip_tags($text);
?>
</textarea>

how the info is updated....

$self = $_SERVER['PHP_SELF'];
if ($_POST['text_id']) { $text_id=$_POST['text_id']; }
if ($_POST['headline']) { $headline=$_POST['headline']; }
if ($_POST['text']) { $text=$_POST['text']; }

$tbl_name = "text_boxes";
$text_id = "27";
if ($logged_in=='true'){
$sql="UPDATE $tbl_name SET headline ='".$headline."', text='".$text."' WHERE text_id = '".$text_id."'";
$result=mysql_query($sql);

Any Ideas? H :^)

Recommended Answers

All 11 Replies

Hey thanks for that but in my text field I still get...

<p><p>WARNING - This email and any attachments may be private & confidential.</p><p>another paragraph etc...<br />another
line break etc....

So I'm trying to get those tags out of the text field and what they do in.

Thanks tho... :^)

By the way, nice function wasn't aware of it.
What's happening is that line breaks and paragraphs
are posted from the text field to the text div as
html tags then echoed and the tags stripped out.

so the text field reads; paragraph,paragraph
and without strip_tags; <p>paragraph</p><p>paragraph</p>

Both ways post to the text div as;

paragraph

paragraph

Which is how I'd like my text field to look and behave.

:^) H

Why don't you use strip_tags like the following?

$input='input<br><b>string</b>';
strip_tags($input,'<p><br>');

That will strip every tag except the <p> and <br> tags. I believe that is the solution to your problem...

Maybe if you nl2br() it before it goes into the db

if ($_POST['text']) { $text=nl2br($_POST['text']); }

then when you want to echo it

$text = strip_tags($text);
echo $text;

That worked fine for me...

But we need a code that will reverse what has been posted
and then selected from and to the database.

The text area posts carrige returns and line breaks to the database
and spits all that into the text div. The tags you suggest I keep are the ones I want to get rid of they will just appear as <p>paragraph</p>.

I'm looking for output - or update to a text field where html tags are output to a textarea just like

this

not like<br />this

I may be barking up the wrong tree but I've done some solid code on this and appreciate your input and output, He He :^) Thanks to date().

I'll try that when I get my eyeballs out from the keyboard. Thanks. :^)

Maybe if you nl2br() it before it goes into the db

if ($_POST['text']) { $text=nl2br($_POST['text']); }

then when you want to echo it

$text = strip_tags($text);
echo $text;

That worked fine for me...

The only problem with inserting the <p> tags is that unless there are two blank lines to represent a removed <p> tag then it would be impossible to reverse because how would the computer know where to insert these tags? It is best not to strip them in the first place.

But we need a code that will reverse what has been posted
and then selected from and to the database.

The text area posts carrige returns and line breaks to the database
and spits all that into the text div. The tags you suggest I keep are the ones I want to get rid of they will just appear as <p>paragraph</p>.

I'm looking for output - or update to a text field where html tags are output to a textarea just like

this

not like<br />this

I may be barking up the wrong tree but I've done some solid code on this and appreciate your input and output, He He :^) Thanks to date().

The only problem with inserting the <p> tags is that unless there are two blank lines to represent a removed <p> tag then it would be impossible to reverse because how would the computer know where to insert these tags? It is best not to strip them in the first place.

Maybe I forget strip tags and use css

#textarea <p>, <br />,
{
display: none;
}

Good thinking, thanks very much, I'll give it a go in the morning. :^)

solved!!!!! This could be really useful to someone out there.
Text area displays

So what if I want to type in this area and apply
changes below.

Make a new line and edit my text some more.

text div displays

So what if I want to type in this area and apply
changes below.

Make a new line and edit my text some more.

text area code (it displays literally, so any whitespace in the code will show in the textarea)

<textarea rows="10" cols="70" name="text" class="text_edit"><?php
$text = preg_replace('/</','
<',$text);
$text = preg_replace('/>/','
>',$text);
$desc = html_entity_decode(strip_tags($text));
$desc = preg_replace('/[\n\r\t]/',' ',$desc);
$desc = preg_replace('/  /',' ',$desc); 
$text = preg_replace('/\r/',' ',$text); 
echo strip_tags($text); 
?></textarea>

solved!!!!! This could be really useful to someone out there.
Text area displays

So what if I want to type in this area and apply
changes below.

Make a new line and edit my text some more.

text div displays

So what if I want to type in this area and apply
changes below.

Make a new line and edit my text some more.

text area code (it displays literally, so any whitespace in the code will show in the textarea)

<textarea rows="10" cols="70" name="text" class="text_edit"><?php
$text = preg_replace('/</','
<',$text);
$text = preg_replace('/>/','
>',$text);
$desc = html_entity_decode(strip_tags($text));
$desc = preg_replace('/[\n\r\t]/',' ',$desc);
$desc = preg_replace('/  /',' ',$desc); 
$text = preg_replace('/\r/',' ',$text); 
echo strip_tags($text); 
?></textarea>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.