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);
Related
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.
For some strange reason, inserting echo "\n"; and other scape sequence characters are not working for me, that's why I am just using <br /> instead.
The images of the results of examples in books and other documentations seems just alright. I'm currently using XAMPP and already used WAMPP with the same actual result. Why is that?
Edit:
It seems that I cannot understand the concept after comparing your answers with this:
PHP Linefeeds (\n) Not Working
Edit:
Sorry I didn't realized that the link above is referring to a php code writing to a file. I just wonder why I have these few php sample programs that uses \n even though it outputs in a webpage. Thanks everyone.
When you run a PHP script in a browser, it will be rendered as HTML by default. If the books you’re using show otherwise, then either the code or the illustration is inaccurate. You can use “view source” to view what was sent to the browser and you’ll see that your line feeds are present.
<?php
echo "Line 1\nLine 2";
?>
This will render in your browser as:
Line 1 Line 2
If you need to send plain text to your browser, you can use something like:
<?php
header('Content-type: text/plain');
echo "Line 1\nLine 2";
?>
This will output:
Line 1
Line 2
PHP Linefeeds (\n) Not Working is referring to sending output to a file rather than the browser.
You should be looking for nl2br(). This will add line breaks (<br>) to your output which will be rendered by the browser; newlines are not.
The echo "\n" is probably working, just not the way you expect it to.
That command will insert a new line character. From the sounds of it, you're using a browser to view your output. Note that if you wrote an HTML file that had a body contents that looked like:
<p>This
is
a
test </p>
The browser rendering would not include the new lines, and would instead just show "This is a test"
If you want to see the newlines, you could view source, and you'll see that the source code includes the new lines.
The rule of thumb is that if you need new lines in a browser, you need to use HTML (e.g. <br />), while if you want it in plain text, you can use the \n
<br /> is the HTML Tag for new line, whereas
"\n" is to output a new line (for real).
The browser doesn't output a new line each time the HTML file goes to the next line.
You can use the nl2br function to convert \n to <br>
As said before, HTML does not render \n as new line. It only recognizes the <br> html tag
If you are working with HTML (viewing the result in browser for example) you have to use the HTML way of linebreaks which is: <br>
/n only works if it is used as a simple text but here as we code in a html doc it takes it as a HTML text hence you can use </br> tag instead.
PHP outputs on the browser and browser only render output in HTML, any other output format will be ignored by the browser.
If you want browser to keep your standard output format as it is, you should enclose your output under HTML's <pre> tag. It preserves the formatting:
echo "<pre>";
echo "This is first line\nThis is new line";
echo "</pre>";
This will be rendered as
This is first line
This is new line
Alternatively, you can mention content type to be plain text in the header:
header('Content-type: text/plain');
echo "This is first line\nThis is new line";
This will tell the browser to render the output as plain text. And the browser will encolse the output automatically in <pre> tag.
solution is echo nl2br or=> <br>
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);
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/>";
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>)