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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

757 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

22 Experts available now in Live!

Get 1:1 Help Now