Solved

How to insert <p> tags into content submitted via <textarea> input?

Posted on 2008-10-22
5
655 Views
Last Modified: 2013-12-13
So, I am aware of all the really nifty (and free) text editor modules that exist out there.  While I could use those to simply my task, all I really want to do is add <p> tags to blank line breaks for content that is typed into a <textarea> input element.

So for example, when a user submits the following into the <textarea>:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam cursus. Morbi ut mi. Nullam enim leo, egestas id, condimentum at, laoreet mattis, massa. Sed eleifend nonummy diam.

 Praesent mauris ante, elementum et, bibendum at, posuere sit amet, nibh.


I would want the HTML that is stored in my database to exist as this:

<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam cursus. Morbi ut mi. Nullam enim leo, egestas id, condimentum at, laoreet mattis, massa. Sed eleifend nonummy diam.</p>

<p>
 Praesent mauris ante, elementum et, bibendum at, posuere sit amet, nibh.
</p>

I'd like to be able to handle this on the server using PHP.  I'm working with a MySQL database.

Thank you!
0
Comment
Question by:ariestav
  • 3
5 Comments
 
LVL 1

Accepted Solution

by:
bluetaraka earned 250 total points
ID: 22779585
easyest way to do this is to put tags at the start and end of your srting and then replace all the line breaks with </p><p>

see below
function addPTags($input)
{
  $output = str_replace("\n", '</p><p>', $input);
  return '<p>' . $output . '</p>';
}

Open in new window

0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 250 total points
ID: 22779709
Macs use a \r for a newline instead of \n. Assuming your textarea has name="comment", then use:



<?php
if( !empty($_REQUEST['comment']) )
{
  $data = '<p>' . preg_replace( '#[\r\n]#' , '</p><p>', $_REQUEST['comment']) . '</p>';
  mysql_connect("localhost","username","password") or die( mysql_error() );
  mysql_select_db("dbname") or die( mysql_error() );
  $query = "INSERT INTO TableName (comments) values('". mysql_real_escape_string($data) . "')";
  mysql_query($query) or die(mysql_error());
}
?>

Open in new window

0
 
LVL 4

Author Comment

by:ariestav
ID: 22780928
Thank you both for this, I will try them both from Mac and PC and let you know how it goes.  I should be able to get to this by the weekend.
0
 
LVL 4

Author Comment

by:ariestav
ID: 22836959
I tried these both, but they did not work.

Hielo:  Empty paragraph tags are inserted on blank lines.  What I originally asked was how I could make this:

Hello World

A new message

Be stored as this:

<p>Hello World</p><p>A new message</p>

But with your code, the message is stored like this:

<p>Hello World</p><p></p><p>A new message</p>

When the user hits the enter key twice I want to assume that they would like to begin a new paragraph.  If the user hits the enter key once, they that is really just a soft return as displayed in the <textarea> tag.

How can I avoid placing the '<p></p>' set where blank lines exist in the <textarea>?

Thanks!

0
 
LVL 4

Author Closing Comment

by:ariestav
ID: 31508874
Thank you very much for your time and help in this question.  I ended up using bluetaraka's solution, but tweaked the string to replace to:

"\r\n\r\n"

to replace blank line.

Thanks!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn how to count occurrences of each item in an array.

777 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