Solved

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

Posted on 2008-10-22
5
656 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

820 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