How do I maintain text layout when using file_get_contents? - php

Let's say I have the following text file:
This is the first line of the text file.
This is the second line,
and here goes the third.
When using
echo file_get_contents($_SERVER{'DOCUMENT_ROOT'} . "/file.txt");
The output is
This is the first line of the text
file. This is the second line, and
here goes the third.
How do I prevent the layout from changing?
Thanks in advance :-)

In HTML, new line characters (\n or \r\n) don't cause actual line breaks to appear in the rendered page. Here are two possible solutions:
Use the nl2br() function to convert newlines to <BR> tags. This will work for some layouts, but not for ASCII art or others that rely on multiple spaces (which are reduced to one in HTML).
echo nl2br(file_get_contents(...));
Wrap the result in <pre> tags. This will keep all layout, but can look a bit ugly. You can style <pre> tags with CSS to make them prettier, if you'd prefer.
echo '<pre>' . file_get_contents(...) . '</pre>';

Every time you are gonna use PHP, it must be done in 2 parts:
Write your page in pure HTML. Make it work as desired. Ask HTML questions if any.
Write a PHP script which produce the the same text.
An HTML tag that could help you is <pre>
Another way to tell to browser that here hoes plain text is to send appropriate HTTP header
So, it this text is the only text being displayed on this page,
header("Content-type: text/plain");
readfile($_SERVER['DOCUMENT_ROOT'] . "/file.txt");

Related

PHP output string and maintain spacing [duplicate]

Any ideas why formatted text from DB, when echo-ed out in php loses its formatting, i.e. no new lines? Thanks!
Use nl2br().
New lines are ignored by browser. That's why you see all text without line breaks. nl2br() converts new lines to <br /> tags that are displayed as new lines in browsers.
If you want to display your text in <textarea>, you don't need to convert all new lines to <br />. Anyway, if you do it... you will see "<br />"s as text in new lines places.
Because there are no html tags for formatting!
Try the nl2br function.
You could try add nl2br() function...
something like this: echo nl2br($your_text_variable);
It should work ;-)
The reason
This is the default behavior for all user agents. If you look at the page source, you'll see that your text has the same formatting like the one in the database (or textarea).
The reason of your confusion is probably that you once see the text in the <textarea> tag, which displays preformatted text, does not interpret the tags, and in the other case the text is interpreted (whitespace is not important in this case).
The browsers don't display new lines, unless specifically asked for - using <br> tag or any block level tags.
No tags == no new lines.
The fix
If you store preformatted text in the database,
you should wrap the output in the <pre> tag.
You may want to convert the formatting characters to the HTML tags you need using set of functions like nl2br, str_replace etc.
You may also correct your structure to store the HTML in the database instead of just plain text (however markup looks like a better solution).
See similar question:
How do I keep whitespace formatting using PHP/HTML?
The difference between the two images you show is that one has the text in a <textarea></textarea> and the other does not ... if you want 1:1: <textarea><?php echo $yourVariable;?></textarea>
It does output what you say to output. If the text is pre-formatted, put it inside the HTML <pre></pre> tag in your output script.
This should be helpful in answering.
How do I keep whitespace formatting using PHP/HTML?enter link description here
Set up a string preprocessing code for both input to database and output to display page

What does exactly mean by 'Line feeds' in HTML and PHP? How do they are added in HTML and PHP code?

I was reading PHP Manual and I come across following text paragraph :
Line feeds have little meaning in HTML, however it is still a good
idea to make your HTML look nice and clean by putting line feeds in. A
linefeed that follows immediately after a closing ?> will be removed
by PHP. This can be extremely useful when you are putting in many
blocks of PHP or include files containing PHP that aren't supposed to
output anything. At the same time it can be a bit confusing. You can
put a space after the closing ?> to force a space and a line feed to
be output, or you can put an explicit line feed in the last echo/print
from within your PHP block.
I've following questions related to the text from above paragraph :
What does exactly mean by 'Line feeds' in HTML?
How to add them to the HTML code as well as PHP code and make visible in a web browser? What HTML entities/tags/characters are used to achieve this?
Is the meaning of 'Line feed' same in case of HTML and PHP? If no, what's the difference in meaning in both the contexts?
Why the PHP manual is saying in first line of paragraph itself that? What does PHP Manual want to say by the below sentence?
"Line feeds have little meaning in HTML"
How can it be useful to remove a linefeed that follows immediately after a closing tag ?> when someone is putting in many blocks of PHP or include files containing PHP that aren't supposed to output anything?
Please someone clear my above mentioned doubts by giving answer in simple, lucid and easy to understand language. If someone could accompany the answer by suitable working code examples it would be of great help to me in understanding the concept more clearly.
Thank You.
What does exactly mean by 'Line feeds' in HTML?
It is a general computing term.
The character (0x0a in ASCII) which advances the paper by one line in a teletype or printer, or moves the cursor to the next line on a display.
— source: Wiktionary
How to add them to the HTML code
Press the enter key on your keyboard. Note that (with a couple of exceptions like <pre>) all whitespace characters are interchangeable in HTML. A new line will be treated as a space.
as well as PHP code
Ditto … or you could use the escape sequence \n inside a string literal.
and make visible in a web browser?
The material you quoted is talking about making source code look nice. You generally don't want line feed characters to be visible in a browser.
You could use a <pre> element instead.
Outside of <pre> elements (and the CSS setting they have by default) you can use a space instead of a new line for the same effect in HTML.
What HTML entities/tags/characters are used to achieve this?
… but the advice given in the last sentence of the material you quoted is probably a better approach.
'Lines feed' exactly means a 'New line' both in Html and Php, only the syntax is different.
In case of Html tag, you can use <br> or <br/> tag for a Lines feed. Basically, this tag shows a new line in the output of the Html attribute block, while running through the browser.
You can take the following example for <br> tag:
<html> <body>
<p> To break lines<br>in a text,<br/>use the br element. </p>
</body> </html>
Output:
To break linesin a text,use the br element.
In case of Php, you can use '\n' for a lines feed.
If you are using a string in Php, then instead of writing,
echo "New \nLine";
you can use nl2br() function to get line break, like:
echo nl2br("New \nLine");
Output:
New
Line

New line ("\n") in PHP is not working

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>

Print text with original spacing

I have a problem with the following:
I want to make a page that gets a file (I upload it), reads it and outputs it in an html file.
I am uploading the file and saving the contents in a mysql DB just fine, but when I show it again, I don't have any <br />'s there (maybe because the file should have \t\n or something.
How can I make it show it like it was originally written. (In the DB I see it with the fine spacing).
You probably want nl2br(). It will transform all line breaks to <br>s
You can either wrap inside <pre></pre> tags to display it as it is, or better yet use nl2br() function to add html break lines <br /> before any newline/carriage return /r /r/n /n
Are you sure the problem isn't just in the HTML? Multiple whitespaces convert to one in web browsers. In modern browsers, you can use the CSS white-space property to prevent that.
body { white-space: pre; }
Alternatively, you could wrap that section of HTML in a <pre> element, or you could hardcode extra spaces into
at time u store file data in database encode data using htmlentities() and at time of displaying decode it using html_entity_decode()

Why does PHP echo'd text lose its formatting?

Any ideas why formatted text from DB, when echo-ed out in php loses its formatting, i.e. no new lines? Thanks!
Use nl2br().
New lines are ignored by browser. That's why you see all text without line breaks. nl2br() converts new lines to <br /> tags that are displayed as new lines in browsers.
If you want to display your text in <textarea>, you don't need to convert all new lines to <br />. Anyway, if you do it... you will see "<br />"s as text in new lines places.
Because there are no html tags for formatting!
Try the nl2br function.
You could try add nl2br() function...
something like this: echo nl2br($your_text_variable);
It should work ;-)
The reason
This is the default behavior for all user agents. If you look at the page source, you'll see that your text has the same formatting like the one in the database (or textarea).
The reason of your confusion is probably that you once see the text in the <textarea> tag, which displays preformatted text, does not interpret the tags, and in the other case the text is interpreted (whitespace is not important in this case).
The browsers don't display new lines, unless specifically asked for - using <br> tag or any block level tags.
No tags == no new lines.
The fix
If you store preformatted text in the database,
you should wrap the output in the <pre> tag.
You may want to convert the formatting characters to the HTML tags you need using set of functions like nl2br, str_replace etc.
You may also correct your structure to store the HTML in the database instead of just plain text (however markup looks like a better solution).
See similar question:
How do I keep whitespace formatting using PHP/HTML?
The difference between the two images you show is that one has the text in a <textarea></textarea> and the other does not ... if you want 1:1: <textarea><?php echo $yourVariable;?></textarea>
It does output what you say to output. If the text is pre-formatted, put it inside the HTML <pre></pre> tag in your output script.
This should be helpful in answering.
How do I keep whitespace formatting using PHP/HTML?enter link description here
Set up a string preprocessing code for both input to database and output to display page

Categories