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
Related
I am inserting this text What is <br/> PHP? into the database
Now I want to show this text as a line break. Like below:
What is PHP?
I am using PHP nl2br() function but it's not working. I am getting the value like this:
What is <br/> PHP?
How can I solve it?
Thank You.
A question comes up here... where are you inserting this text (string) ?
If you are injecting it as HTML you'll get the desired result in the rendered page.
I assume this is not the case: you want HTML line breaks turned into newlines.
So...
nl2br()converts newlines into <br />: that's the opposite you want to do
http://php.net/manual/en/function.nl2br.php
Just use str_replace:
$out = str_replace( "<br/>", "\n", $in );
Where $in is the input string and $out is the desired output
http://php.net/manual/en/function.str-replace.php
Just a couple of things to note:
1) the above code will work with HTML line breaks <br/>, not if you have <br>, or <br />
If this is an issue you may pass the function array of strings and array of their replacements. This is well documented in the link above.
2) If you use the code snipped I wrote above you'll end having two spaces in the resulting string:
What is (with trailing space)
PHP (with leading space)
Just use css instead of server-side transformations:
p {
white-space: pre-line;
}
<p>I am inserting this text What is
PHP? into the database
</p>
<p>Now I want to show this text as a line break. Like below:</p>
<p>What is
PHP?</p>
I'm trying to make a list of email addresses (each one on a new line). I pull the array from my database, explode it along the comma delimiter, and run it through a foreach loop.
$emailsList = "";
foreach($emails as $email)
{
$emailsList = $emailsList . "\n" . $email;
}
echo "Additional Report Emails<textarea name='showReportsEmails'>".$emailsList."</textarea>";
When I look in the textarea itself, it will literally show the line break tag in the textarea. What can I do to get rid of this and have the text behave as I want?
<br />
email1#gmail.com<br />
email2#gmail.com
Thanks!
It looks like when the result is printed in the textarea, the PHP new lines (\n) are converted to <br/>s. So, maybe there's a nl2br() function being used to output the result into the textarea. It should be fixed by removing that. Otherwise, you can use the code below when setting the textarea's content:
<textarea><?php echo implode("\n", array_map('trim', explode("\n", strip_tags($emailsList)))); ?></textarea>
The code above first removes all html tags from the $emailsList variable, and then also removes white spaces before and after lines, so you would be good to go with this.
You should be using Carriage returns instead of HTML code in the textarea.
\r
The name suggest the the element can only hold text. It doesn't parse as HTML rather it does so as text.
I tested this code:
<?php
$value = "Hello,\nname\nis\nScript47.";
echo "<textarea>$value</textarea>";
?>
Output
Edit 1:
You could also use \r to see if it helps.
Please can someone tell me where I'm going wrong?
//outputs : Hi, my name is Jackal I like PHP
<?php
echo "Hi, my name is Jackal \nI like PHP";
?>
Whereas if I use pre tag
//outputs: Hi, my name is Jackal
// I like PHP
<pre>
<?php
echo "Hi, my name is Jackal \nI like PHP";
?>
Can someone please explain why character escaping isn't working?
thanks
PHP is interpreting that line break. Look in the source code of the webpage. You'll see that you have 2 lines in the source.
However, HTML handles line breaks differently. To perform a line break in HTML, use the <br> tag. This will make the HTML output over 2 lines on the webpage itself. However, in the source code, it will still appear as a single line (unless you add that line break).
What <pre> is doing is telling the HTML engine to output the exact string as-is since the text is preformatted, including interpreting that line break. Also note, you should add a closing </pre> tag at the end of the block that is preformatted.
If you have this in your HTML:
<p>Hello,
World!</p>
Does it appear on one line, or two?
Usually, the answer is one: whitespace is condensed into a single space.
<pre>, however, has the default CSS of white-space:pre, which does NOT condense whitespace.
You should echo "<br />", or apply white-space:pre (or, even better, white-space:pre-wrap) to the container element.
Because in your browser \n shows just a whitespace and does not appear as line break unless you use pre-formatted HTML tag <pre>.
e.g. following will display with a line break in browser:
echo "Hi, my name is Jackal <br />I like PHP";
or this one also:
echo "<pre>Hi, my name is Jackal \nI like PHP</pre>";
As you can make out that <pre> tag is for displaying pre-formatted text hence \n does show as line break.
Character escaping is working. Look at the source code, there you'll find the new line. In order to make it visible in a browser you need an html tag (<br />) or the wrapping <pre>
<br>
is HTML.
<br />
is XHTML (recommended).
\n
is a newline in the code (works while writing to file, but doesn't output to the screen).
alternately you can use nl2br as below
echo nl2br("Hi, my name is Jackal \nI like PHP");
The newline character in php actually echos a new line itself.
However, the output of php's echo statement is still the source code of the web browser. Hence, the browser condenses all the extra line breaks and spaces. For example:
<p>Hello
there</p>
outputs
Hello there
despite the white space. To get around this issue, use the <br>/<br/> tag to tell the browser to make a newline, instead of tell php to make a newline.
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.
i'm writing this:
echo "foo";
echo "\n";
echo "bar";
and "bar" is not written in the line below.
What am i doing wrong?
Javi
Newlines in HTML are expressed through <br>, not through \n.
Using \n in PHP creates a newline in the source code, and HTML source code layout is unconnected to HTML screen layout.
If you want a new line character to be inserted into a plain text stream then you could use the OS independent global PHP_EOL
echo "foo";
echo PHP_EOL ;
echo "bar";
In HTML terms you would see a newline between foo and bar if you looked at the source code of the page.
ergo, it is useful if you are outputting say, a loop of values for a select box and you value having html source code which is "prettier" or easier to read for yourself later. e.g.
foreach( $dogs as $dog )
echo "<option>$dog</option>" . PHP_EOL ;
If you want to write plain text, you must ensure the content type is set to Content-Type: text/plain. Example:
header('Content-Type: text/plain');
If you are dealing with HTML, you have two options. One is to inset a new line using <br> (Or <br /> for XHTML). The other is to put the plain text in a <pre> element (In this case "pre" stands for preformatted).
PHP generates HTML. You may want:
echo "foo";
echo "<br />\n";
echo "bar";
if your text has newlines, use nl2br php function:
<?php
$string = "foo"."\n"."bar";
echo nl2br($string);
?>
This should look good in browser
Assuming you're viewing the output in a web browser you have at least two options:
Surround your text block with <pre> statements
Change your \n to an HTML <br> tag (<br/> will also do)
Since it wasn't mentioned, you can also use the CSS white-space property
body{
white-space:pre-wrap;
}
Which tells the browser to preserve whitespace so that
<body>
<?php
echo "hello\nthere";
?>
</body>
Would display
hello
there
We can use \n as a new line in php.
Code Snippet :
<?php
echo"Fo\n";
echo"Pro";
?>
Output:
Fo
Pro
It will be written on a new line if you examine the source code of the page. If you want it to appear on a new line when it is rendered in the browser, you'll have use a <br /> tag instead.
This works perfectly for me...
echo nl2br("\n");
Reference: http://www.w3schools.com/php/func_string_nl2br.asp
Hope it helps :)
This answer was so obvious and it took me forever to figure out:
echo "foo
bar";
I know that looks like it's wrapping. It's not. What I did is I literally hit return halfway through the string, between foo and bar. It creates a new line in the HTML source and makes your PHP look horrible. This was in Linux/Apache.
echo "foo<br />bar";
We can apply \n in php by using two type
Using CSS
body {
white-space: pre-wrap;
}
Which tells the browser to preserve whitespace so that
<body>
<?php
echo "Fo\n Pro";
?>
</body>
Result:
Fo
Pro
Using nl2br
nl2br: Inserts HTML line breaks before all newlines in a string
<?php
echo nl2br("Fo.\nPro.");
?>
Result
Fo.
Pro.
the html element break line depend of it's white-space style property.
in the most of the elements the default white-space is auto, which mean break line when the text come to the width of the element.
if you want the text break by \n you have to give to the parent element the style:
white space: pre-line, which will read the \n and break the line, or
white-space: pre which will also read \t etc.
note: to write \n as break-line and not as a string , you have to use a double quoted string ("\n")
if you not wanna use a white space, you always welcome to use the HTML Element for break line, which is <br/>
$a = 'John' ; <br/>
$b = 'Doe' ; <br/>
$c = $a.$b"<br/>";