Solved

How do I append <p> and <br> tags to $_POST["form_variables"] I receive from a text area?

Posted on 2003-11-11
2
321 Views
Last Modified: 2006-11-17
A user types multiple paragraphs in a text area, and then submits it to a php page.  Is there an easy way (similar to addslashes() ) that I can append the <p> tag when they hit "Enter" twice and just the <br> tag when they hit "Enter" once?

I know that I could loop through $_POST variable searching for the newline character, but I didn't want to do this if there is already an inherent PHP function that does it for me.  Also, if in a worse case scenerio I have to look for and replace the characters with HTML tags, what is the character for new line?

"\n" is the escape character for display, but if my text area is as follows:

Line one
Line two

and that gets posted to a page, what do I look for in $_POST["variable"] that represent a carriage return?  Is it still the "\n".  

Maybe there is a regular expression that would replace all carriage returns with <br> and all double carriage returns with <p>?  Any help is very much appreciated.

Thanks,
Scott.
0
Comment
Question by:kbach
2 Comments
 
LVL 13

Accepted Solution

by:
lozloz earned 200 total points
ID: 9725466
hi,

use the function nl2br();

e.g.

$variable = nl2br($_POST["variable"]);

this simple adds a <br /> tag where there's a \n - you don't really need to worry about double carriage returns being converted to <p>, this function is pretty standard i imagine. however if you're putting the post information into a database, don't do nl2br(); until you're extracting the data for display in HTML format, then you'll be able to edit it in a textarea without having the <br /> tags there

loz
0
 
LVL 1

Author Comment

by:kbach
ID: 9725755
Perfect!

Thanks alot.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

932 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

15 Experts available now in Live!

Get 1:1 Help Now