TextArea line breaks for e-mail - php

How can I take the text from a textarea (html) and insert line breaks. Right now, if I input info to send out as an e-mail, it puts all the text on the same line without any line breaks.
Using $_POST['field'] to get the data from the form and sending using PHP mail.

Use nl2br() function. It replaces all newlines within a string with html br tags.

use \n for new line, or \r\n for return followed by new line
ie.
<?php
printf("This is the first line. \n");
printf("This is the second line");
?>
ie. to replace html tag with newline:
str_replace ('<br>' , '\r\n', $_POST['field'])
alternativly set the email you are sending out to be html encoded (add html header)

In php, replace \n with html br tag,
$newTxt = str_replace("\n",'<br>',$txt)
or nl2br() will serve your purpose.

remove stripslashes(), this will work.

Related

Print text including line returns in PHP

I am pulling text from a database and I want to print out the text and include the line returns so the text is readable and not all compressed into one paragraph.
Is this possible and if so what is the PHP function.
Thank you in advance.
The problem you have is that you get \n out of the database. Because HTML ignores whitespace, then those \n will be ignored by your browser.
You can use the nl2br function to convert all \n to <br /> tags.
you can use the HTML < pre > tag or nl2br($string)
You could either change the header to text/plain:
header("Content-type: text/plain");
echo $db_text;
Or you could replace new lines with breaks:
echo nl2br($db_text);

PHP, strip_tags stripping \n in text area. How to stop it?

I would like to be able to accept \n or \r\n and convert them to <br /> for use in the page. Although when a user submits a text area with new paragraphs, the strip_tags function seems to strip them right out. Anything I can do to keep these in the string?
Thanks!!!
You can use nl2br to add the BR line break element to line break character sequences:
$html = nl2br($plain);
Note that the BR elements are just added:
nl2br("foo\nbar") === "foo\n<br />bar"
And to prevent strip_tags to remove P and BR tags, specify them in the second parameter:
$clean = strip_tags($html, '<p><br>');

line breaks on comments textareas

A user post a comment on a forum using a textarea, he presses enter to format his text which means line breaks, how can I convert those line breaks to html breaks when inserting its message on the database so the message is echoed with the line breaks formatting?
There's a function called nl2br(). Use it when echoing data, not when inserting to database. You don't want to display those <br/> tags when the user opens their post in textarea for editing, do you?
Use nl2br()
It will convert new lines to <br>, so html will understand it.
Use nl2br() function. It replaces all newlines within a string with html br tags.
For example (taken from php manpage):
<?php
echo nl2br("foo isn't\n bar");
?>
Outputs:
foo isn't<br />
bar
The best practice is to use it when outputting data and store the data in the original ofrmat (with standard newlines).
You mean this?:
$html_text = nl2br($text);

Problem printing PHP output on multiple lines

I am newbie to PHP I have written the following program:
$address=array('abc#gmail.com','abc#hotmail.com','def#yahoo.com');
foreach($address as $value)
{
echo "processing $value\n";
}
If you see I have \n in the echo statement but I am not getting the output on new line.
How can I get each output on a new line?
If you are outputting this as HTML then you must of course use a HTML break <br />.
If you're working in a browser, you need to break lines with
<br>
You need to print an HTML line break instead:
<br/>
Since you are printing to a browser
\n will line break properly when you view the source, but not in the HTML display. As mentioned, you need to use the <br/> node for HTML
You may want to wrap your output in a <pre> tag as your browser is expecting HTML and is just collapsing the whitespace. The pre tag will reflect the whitespace (\t \n etc);
Alternately you can use a break tag, or wrap the data in a block display element. (eg: <p> or <div>)

about html textarea's post value

text file in window, each line end with \r\n, in unix, each line end with \n.
Does textarea's post value follow this rule?
To make it simple: yes
Usually you just want to replace \n by <br /> and that does the trick.
On Windows with Opera, Firefox and IE7 it ends the text lines with \r\n. I presume in Unix will be only \n, but don't have a system to test now.
a modified function I've used from php.net commetns to replace \n and \r into one {or whatever you pass in}
function replaceNewLines($string,$replacement='<br />')
{
return preg_replace("/(\r\n)+|(\n|\r)+/", $replacement, $string);
}
$string = "this is \n\n\n a String with many \n\n\r\r returns!";
$string = replaceNewLines($string,'');
When you press enter when typing in a textarea, it creates just a \n.
If you want to then display the data (formatted with line breaks) on a page, you need to to a str_replace on the text from your textarea to replace the \n's.
If processing is done by php there is a php function called nl2br that will do the trick. It takes two argument, first is your string, second is a bool true for xhtml and false for html.
http://www.php.net/manual/en/function.nl2br.php
Example from php.net:
<?php
echo nl2br("foo isn't\n bar");
echo nl2br("Welcome\r\nThis is my HTML document", false);
?>
The above example will output (first is xhtml, second plain html):
foo isn't<br />
bar
Welcome<br>
This is my HTML document

Categories