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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

773 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