开发者

browsers do not read new line in a php file

I have a password protected 开发者_StackOverflowtext file and to make it password protected, i used a password protector script (which works great) but it required me to rename the text file to .php on my server. This went fine, however, when I open this text file in any browser on windows, i do not seeing any new lines (I used to see them)

I tried writing -"\n", "\r", "\r\n". I think it has to do with the browser thinking its a .php file i guess.


This is because the server is sending a different MIME type. It is now sending text/html (the default type returned by PHP) rather than text/plain.

Your browser is then expecting HTML. Line breaks are just like any other white space in HTML, so they are essentially meaningless for what you are trying to do.

You can use this to fix it:

header('Content-Type: text/plain');

Be sure to put that at the top of your code, or at least before you output anything.

This causes the server to send the MIME type you are expecting.


By default the output of PHP scripts are rendered as HTML, which means that whitespace is folded. If you want to change this back to text then you need to set the Content-Type header to "text/plain", either in the web server or via the header() function.


That's because the browser would see the content as html and in html a newline is just a whitespace


I am not sure if I understood your question properly, but in two cases there is a solution:

-You output the text: In this case, you have to use

<br>

-You want to write it with new lines in the file: Use the PHP-Constant

PHP_EOL

which means End-of-Line. This inserts always a correct break.


You need to use <br>, as html is being rendered in the browser.


Browsers uses HTML to format text (contained in html of course)

use <br> or <p>

Also considering your file is a .php it's a normal behaviour that your webserver will send it as text/html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜