why does all the text get printed on the same line? [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Have I misunderstood what heredoc should do?
I read that Here document preserves the line breaks
and other whitespace (including indentation) in the text. But when I run the following script,everything gets printed on the same line. Why is it so ?
<?php
$str = <<<HDC
This is a sample text
Some more sample text
Even more sample text
HDC;
echo $str;

The output actually does contain the line breaks. However, HTML (by default) ignores line breaks.
If you want the HTML to render the line breaks, wrap it in a pre:
<pre>
<?php
$str = <<<HDC
This is a sample text
Some more sample text
Even more sample text
HDC;
echo $str;
?>
</pre>

Or if you cannot use <pre> because <pre> always prints in fixed width, manually insert the <br> tags.

Related

PHP doesn't render properly in Web Browser [duplicate]

This question already has answers here:
new lines in PHP. how?
(3 answers)
how to write new lines in CLI and web browser?
(5 answers)
PHP - how to create a newline character?
(15 answers)
Closed 2 years ago.
Sample PHP code
wolf#linux:~$ cat 1-new-line.php
<?php
echo "Hello
World
Foo Bar!";
$aString = '
First Line
Second Line
Third Line';
echo $aString;
?>
wolf#linux:~$
It runs as expected in CLI
wolf#linux:~$ php 1-new-line.php
Hello
World
Foo Bar!
First Line
Second Line
Third Line
wolf#linux:~$
But not via browser
What's wrong in this code? How to render it properly in web browser?
Browsers interpret PHP's output as HTML, and in HTML newlines get replaced by spaces.
If you want to learn how to line-break in a browser, use <br /> or wrap sentences in <p> tags.
You can also tell a browser to treat your output as text, and not HTML. In that case, use the following header (before any output):
header('Content-Type: text/plain');
You are rendering it in a browser without any HTML formatting. Wrapping it in a <pre></pre> tags will preserve its plaintext formatting (newlines and whitespace) when rendered in the browser.
<pre>
<?php
echo "Hello
World
Foo Bar!";
</pre>
$aString = '
First Line
Second Line
Third Line';
<pre>
echo $aString;
</pre>
?>

How to show line break using PHP?

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>

Present Text file on webpage

I have a text file "applications.txt" I want to show on the web using PHP, I have this line of code:
<?php
$myfilename = "applications.txt";
if(file_exists($myfilename)){
echo file_get_contents($myfilename);
}
?>
The contents of the text file looks like this:
Line one
Line two
Line three
However, on the webpage, it looks like this:
Line one Line two Line three
How do I make it display the new lines properly?
This is happening because HTML ignores more than two concurrent whitespaces and treats them as a single one. You can wrap the output in <pre> tags
echo "<pre>" . file_get_contents($myfilename) . "</pre>";
that will preserve the file as it appears but can lead to problems for files with long lines. You can also replace newlines with <br/> tags using nl2br()
echo nl2br(file_get_contents($myfilename));
HTML uses <br/> markup to make a line return, you should either do a foreach line and display a markup, or use a pre markup to disregard formatting.

How do you keep the whitespace and formatting in a textarea form post? [duplicate]

This question already has answers here:
Preserve Line Breaks From TextArea
(7 answers)
Closed 9 years ago.
I am trying to post a text area which I am storing into my database and I want to keep all of the white space so that I can query it out and display at a different place. The problem here is that when this form submits, the entire textarea is sent as a single line. I've looked into htmlspecialchar and urlencode but how can that be applied here if this is even the right place to use them?
<form name = "exam" action="takeTest.php" method="post">
<textarea name="pa1" required rows=10 cols=55>public int add(int a, int b){
}
</textarea>
<input type="submit">
</form>
EDIT
Tried pre tags but those just show up as the default text in addition to the code. I think adding the \n tags will mess up the code when it's put through unit testing but I'm going to try it.
This part of my form is the programming part which is trying to emulate what codingbat is doing.
Like #Danny said, the lines are separated, but not with the HTML line breaks, as you might expect.
To turn the newline characters (\n) into HTML line breaks (<br>), simply use nl2br; so you can use your string from the database in a proper HTML.
It converts:
Hey\nthere
into:
Hey<br>there
which browsers render like:
Hey
there
before insert in db, php has a function nl2br().
then, if you'll echo the data on body just print else for editing this again in textarea use this
function br2nl($string)
{
return preg_replace('/\<br(\s*)?\/?\>/i', "\n", $string);
}

PHP won't output new line [duplicate]

This question already has answers here:
PHP - how to create a newline character?
(15 answers)
Closed 9 years ago.
I've tried every method to output a newline in PHP. Why doesn't the following work? :
<?php
$foo = 'bar';
echo "Hello \n $foo!";
?>
This should output a newline between hello and bar but it isn't.
I also tried \r\n instead of \n
If you using it as command line script this would work. I would use PHP_EOL for this since it chooses the right line break for the OS.
However 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 />
To output a new line in HTML you need to use HTML's representation of a new line which is <br/>
php has a function for you that converts all natural new lines to HTML new lines > nl2br()
Then your code should look like
<?php
$foo = 'bar';
echo nl2br("Hello \n $foo!");
?>
Use <br> when inserting line break in html
To output a new line visually (in a browser), you need HTML:
echo "Hello\n<br />$foo!";
\n is a system line feed.
Try this:
<?php
$foo = 'bar';
echo "Hello <br> $foo!";,
?>
if you view the source you will see it actually DOES have a new line in it, But white space doesnt matter in HTML (what the browser displaying the output is expecting) but adding a <br /> to your statement will produce a new line because this is the way browsers read new lines and display them.

Categories