Solved

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

Posted on 2008-10-22
5
658 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
[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
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

630 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