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
327 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Mail error 3 43
Showing multiple maps using PHP and Google Maps 12 38
How Close unsubmited attempts 10 43
Find RGB colors from a screen. 2 16
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 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