Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-10-22
5
Medium Priority
?
662 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 750 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 750 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
The viewer will learn how to dynamically set the form action using jQuery.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

876 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