Paragraphs after inserting text using a text area

I need to find a way to have text inserted true a basic textarea to become paragraphs without using the <p></p> tags. Before I used Coldfusion and they have a cusom text area that was doing that with the all text. So if you typed some text and gave a hard return and typed some more text. It became two paragraphs in the database. Can this be done in PHP as well or do I need to integrate some Javascript/jQuery?

Thank you in advance!

Hi donboe,

are you asking how to format input received from a text area before inserting it into the database?

Hi Pullo. Yes that is what I am asking, creating paragraphs without using the <p> & </p> tags

Ok then, couldn’t you just use a regex to swap line breaks for whatever your desired formatting is?

I was looking into that but what about the first paragraph. I just start typing withou a line break. This is for a client not for me. I wouldn’t have a problem with that! I need tha <p> tag for the first paragraph as well since that is how I have declared things in my css

Can you provide an example of what is being submitted via your form and then a second example of what you would like to convert it to before entering it in the database.
I have a feeling I’m not quite understanding what you are wanting to do.

Hi Pullo. Just like i am entering text here on Sitepoint. I start typing a text

and now I started two lines below. Here on Sitepoint that would give me two paragraphs.

What I need to is having this inserted into the database without having t use the the <p> tags. I cant ask that from clients!

Edit: I could adjust my CSS a bit though!

So when you have this:

Some text\
\
Some more text\
\
Even more text

You need this:

<p>Some text</p><p>Some more text</p><p>Even more text</p>

I’m still not sure i understood this right.

Yes that is what i am after (this is how I would like to see it front-end) but backend (in the text area) it should not give me those <p></p> or

. But how to accomplish that with a standard text area?

Sorry donboe, I really don’t understand what you are trying to accomplish.

What I have understood, so far:

You have a form.
In the form is a text area.
The form is submitted to a server-side PHP script which inserts your text into a database.

When you enter the following into the text area:

Text

Text

Text

It arrives at your PHP script like this:

Text\
\
Text\
\
Text

(the \ character representing a line break)

What I have not understood:
What do you want to do with it before inserting it to the database?

I have to head off soon, so will be offline in about 15 minutes.

I think he wants to do something like this.

<?php
$sometext = "Some text\
\
Some more text\
\
Even more text";
$output = "<p>" . str_replace("\
\
", "</p><p>", $sometext) . "</p>";
echo $output;
?>

Yup. My thoughts, too.

I believe the values for return are different based on system the code is running on. Might need to be
\r in some cases.

$output = "<p>" . str_replace("\
\\r", "</p><p>", $sometext) . "</p>";

Text

Text

Text this is indeed how I need it in the database. But on the frontend will this represent:
<p>Text</p>
<p>Text</p>
<p>Text</p>

So now are you echoing this text in the text area? Is that what you mean by front end?

You are right. Maybe I did not explain it right. With frontend I mean how it show on the website, but also in the controlroom (the textarea) the cliens should not have to deal with any <p> or whatever tags

Then it’s probably best to store POSTed text in the DB as sent (In field type text) then use the <p> formatting where needed.

I know this topic has been covered many times in many places.
This POST looks like a good example.
It takes into consideration single line breaks and goes like this.

# first, lets trim starting/trailing whitespace
$text = trim($sometext);

# temporarily replace two or more consecutive newlines
# into SOH characters (not used in normal texts)
$text = preg_replace('~(\\r\
|\
){2,}|$~', "\\001", $text);

# convert remaining (i.e. single) newlines into html br's
$text = nl2br($text);

# finally, replace SOH chars with paragraphs
$text = preg_replace('/(.*?)\\001/s', "<p>$1</p>\
", $text);

# test
echo $text;

And a working example

<html>
<body>
<?php
$sometext = (isset($_POST['sometext']) ? $_POST['sometext'] : '');

# first, lets trim starting/trailing whitespace
$text = trim($sometext);

# temporarily replace two or more consecutive newlines
# into SOH characters (not used in normal texts)
$text = preg_replace('~(\\r\
|\
){2,}|$~', "\\001", $text);

# convert remaining (i.e. single) newlines into html br's
$text = nl2br($text);

# finally, replace SOH chars with paragraphs
$text = preg_replace('/(.*?)\\001/s', "<p>$1</p>\
", $text);

# test
echo $text;
?>
<form method="post" action="" class="validate">
<textarea name="sometext"><?php echo $sometext; ?></textarea>
<input type="submit" value="Submit" name="submit" />
</form>

</body>
</html>

This is a very nice example which I indeed can use and I think I start to understand the principle but I am still confused but that is most definately because I did not explain myselfthe right way. I will try it again.

I don’t have a problem inserting the text in the database with line breaks by using nl2br:

Some text\
\
Some more text\
\
Even more text

Thanks to you guys (Drummin & Pullo) I don’t have a problem either to echo that result in a proper way on the website convered to paragraphs:

$output = "<p>" . str_replace("\
\
", "</p><p>", $sometext) . "</p>"; 

Where I have a problem with though is how to echo this result in the text area when the site owner want to update the text. When I echo the row in the the text area it shows up like:

Some text\
\
Some more text\
\
Even more text

But like i said I don’t want the client to see those

's What I am looking for is that the text in the text area show like this:

Some text

Some more text

Even more text

I hope I make myself a bit clearer this way

Ok, I’ve been playing around with this.

I made a simple demo page:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>DB fun</title>  
  </head>
  <body>
    <form method="post" action="donboe_db.php"> 
    <textarea name="sometext" rows="10" cols="50"></textarea> 
    <input type="submit" value="Submit" name="submit" /> 
    </form> 
  </body>
</html>

On this page I entered the following into the text area:

Some text

More text

Even more text

I submitted the form to a small PHP script, which inserted it into the database:

<?php 
  $conn = new PDO('mysql:host=localhost;dbname=sitepoint', '****', '****');
  $sometext = $_POST['sometext'];
  $sql = "INSERT INTO donboe (sometext) VALUES (:sometext)";
  $q = $conn->prepare($sql);
  $q->execute(array(':sometext'=>$sometext));
?>

Then I dumped the table and got this:

--
-- Table structure for table `donboe`
--

CREATE TABLE IF NOT EXISTS `donboe` (
  `sometext` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `donboe`
--

INSERT INTO `donboe` (`sometext`) VALUES
('Some text\\r\
\\r\
Some more text\\r\
\\r\
Even more text');

As you can see, the line breaks from the text area ended up as \\r\ , presumably as I’m on Linux.

I then modified my initial script to fetch the value from the db and display it in the text area:

<?php 
  $pdo = new PDO('mysql:host=localhost;dbname=sitepoint', '****', '****');
  $select = 'SELECT sometext';
  $from = ' FROM donboe';
  $sql = $select . $from;
  $statement = $pdo->prepare($sql);
  $statement->execute();
  $results=$statement->fetchColumn();
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>DB fun</title>  
  </head>
  <body>
    <form method="post" action="donboe_db.php"> 
    <textarea name="sometext" rows="10" cols="50"><?php echo $results; ?></textarea> 
    <input type="submit" value="Submit" name="submit" /> 
    </form> 
  </body>
</html>

And I saw:

Some text

More text

Even more text

formatted correctly.

I’m not sure how this helps you, but it was interesting that I couldn’t reproduce your issue at all.

To move this forward:
Could you do a dump of your DB table and let us see how the line breaks are being stored.
Please post the relevant parts of the results here.