Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
332 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 13

Accepted Solution

by:
lozloz earned 800 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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.
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 …

688 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