Solved

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

Posted on 2008-10-22
5
652 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now