通过 PHP 呈现纯文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4506679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:14:12  来源:igfitidea点击:

Rendering plain text through PHP

phptextheaderrobots.txtplaintext

提问by RabidFire

For some reason, I want to serve my robots.txt via a PHP script. I have setup apache so that the robots.txt file request (infact all file requests) come to a single PHP script.

出于某种原因,我想通过 PHP 脚本提供我的 robots.txt。我已经设置了 apache,以便 robots.txt 文件请求(实际上是所有文件请求)进入单个 PHP 脚本。

The code I am using to render robots.txt is:

我用来渲染 robots.txt 的代码是:

echo "User-agent: wget\n";
echo "Disallow: /\n";

However, it is not processing the newlines. How to server robots.txt correctly, so search engines (or any client) see it properly? Do I have to send some special headers for txt files?

但是,它不处理换行符。如何正确服务器 robots.txt,以便搜索引擎(或任何客户端)正确查看它?我是否必须为 txt 文件发送一些特殊的标题?

EDIT 1:

编辑 1:

Now I have the following code:

现在我有以下代码:

header("Content-Type: text/plain");
echo "User-agent: wget\n";
echo "Disallow: /\n";

which still does not display newlines (see http://sarcastic-quotes.com/robots.txt).

它仍然不显示换行符(参见http://sarcastic-quotes.com/robots.txt)。

EDIT 2:

编辑2:

Some people mentioned its just fine and not displayed in browser. Was just curious how does this one display correctly: http://en.wikipedia.org/robots.txt

有些人提到它很好,没有显示在浏览器中。只是好奇这个如何正确显示:http: //en.wikipedia.org/robots.txt

EDIT 3:

编辑 3:

I downloaded both mine and wikipedia's through wget, and see this:

我通过 wget 下载了我的和维基百科的,并看到了这个:

$ file en.wikipedia.org/robots.txt
en.wikipedia.org/robots.txt: UTF-8 Unicode English text

$ file sarcastic-quotes.com/robots.txt
sarcastic-quotes.com/robots.txt: ASCII text

FINAL SUMMARY:

最终总结:

Main issue was I was not setting the header. However, there is another internal bug, which is making the Content-Type as html. (this is because my request is actually served through an internal proxy but thats another issue).

主要问题是我没有设置标题。但是,还有另一个内部错误,就是将 Content-Type 设为 html。(这是因为我的请求实际上是通过内部代理提供的,但那是另一个问题)。

Some comments that browsers don't display newline were only half-correct-> modern browsers correctly display newline if content-type is text/plain. I am selecting the answer that closely matched the real problem and was void of the above slightly misleading misconception :). Thanks everyone for the help and your time!

一些关于浏览器不显示换行符的评论只对了一半-> 如果内容类型是文本/纯文本,现代浏览器会正确显示换行符。我正在选择与实际问题密切匹配的答案,并且没有上述略有误导性的误解:)。感谢大家的帮助和您的时间!

thanks

谢谢

JP

J.P

回答by RabidFire

Yes, you forgot to set the Content Type of your output to text/plain:

是的,您忘记将输出的内容类型设置为text/plain

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

Your output is probably being sent as HTML, where a newline is truncated into a space, and to actually display a newline, you would need the <br />tag.

您的输出可能作为 HTML 发送,其中换行符被截断为空格,并且要实际显示换行符,您需要<br />标签。

回答by Neil E. Pearson

  1. header('Content-Type: text/plain')is correct.
  2. You must call this method beforeanything is written to your output, including white space. Check for whitespace before your opening <?php.
  3. If your Content-Typeheader has been set to text/plain, no browser in its right mind would collapse whitespace. That behaviour is exclusive to HTML and similar formats.
  4. I'm sure you have your reasons, but as a rule, serving static content through PHP uses unnecessary server resources. Every hit to PHP is typically a new process spawn and a few megs of memory. You can use apache config directives to point to different robots files based on headers like User-Agent- I'd be looking into that.
  5. It's likely that search engines ignore the Content-Typeheader, so this shouldn't be an issue anyway.
  1. header('Content-Type: text/plain')是正确的。
  2. 您必须在将任何内容写入输出之前调用此方法,包括空格。在您打开之前检查空格<?php
  3. 如果您的Content-Type标头已设置为text/plain,那么头脑正常的浏览器不会折叠空白。这种行为是 HTML 和类似格式独有的。
  4. 我相信您有自己的理由,但通常情况下,通过 PHP 提供静态内容会使用不必要的服务器资源。对 PHP 的每次攻击通常都会产生一个新进程和几兆内存。您可以使用 apache 配置指令根据标头指向不同的机器人文件,例如User-Agent- 我会研究一下。
  5. 搜索引擎很可能会忽略Content-Type标题,因此无论如何这都不是问题。

Hope this helps.

希望这可以帮助。

-n

-n

回答by deceze

<?php header("Content-Type: text/plain"); ?>
User-agent: wget
Disallow: /

BTW, the newlines are there just fine. They're just not displayed in a browser. Browsers collapse all whitespace, including newlines, to a single space.

顺便说一句,换行符在那里就好了。它们只是没有显示在浏览器中。浏览器将所有空格(包括换行符)折叠为一个空格。

deceze$ curl http://sarcastic-quotes.com/robots.txt
User-agent: wget
Disallow: /

回答by Matthew Scharley

You must set the content type of the document you are serving. In the case of a .txt text file:

您必须设置您所服务的文档的内容类型。对于 .txt 文本文件:

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

The IANA has information about some of the more popular MIME (content) types.

IANA 有关于一些更流行的 MIME(内容)类型的信息

回答by anakin

i was having a similar issue and either "\n" nor PHP_EOL worked. I finally used:

我遇到了类似的问题,无论是“\n”还是 PHP_EOL 都有效。我终于使用了:

header('Content-Disposition: attachment; filename="plaintext.txt"');
header("Content-Type: text/plain");
echo "some data";
echo chr(13).chr(10);

The echo of BOTH characters did the trick. Hope it helps someone.

两个角色的回声都起到了作用。希望它可以帮助某人。

Bye anankin

再见阿南金

回答by Thomas Havlik

If you are using echo, then use <br>for new lines. the printf function is what uses \n.

如果您使用的是回声,则用于<br>换行。printf 函数就是使用\n 的。

In your case, use printf because you are not using HTML. I believe this is the proper way to do this, along with setting the MIME type to text.

在您的情况下,请使用 printf 因为您没有使用 HTML。我相信这是正确的方法,同时将 MIME 类型设置为文本。