通过PHP呈现纯文本 [英] Rendering plain text through PHP

查看:149
本文介绍了通过PHP呈现纯文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  echoUser-agent:wget\\\
;
回显Disallow:/ \\\
;

然而,它并没有处理换行符。如何正确地服务robots.txt,以便搜索引擎(或任何客户端)正确地看到它?我需要为txt文件发送一些特殊的头文件吗?



编辑1:



现在我有以下code:

  header(Content-Type:text / plain); 
echoUser-agent:wget\\\
;
回显Disallow:/ \\\
;

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

编辑2: / h2>

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

编辑3:



我通过wget下载了mine和wikipedia,并且看到了这个: p>

  $文件en.wikipedia.org/robots.txt 
en.wikipedia.org/robots.txt:UTF-8 Unicode英文文本

$文件sarcastic-quotes.com/robots.txt
sarcastic-quotes.com/robots.txt:ASCII文本



总结:



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



有些浏览器不显示换行符的注释只有一半,正确 - >现代浏览器正确显示换行符,如果内容类型是文本/纯文本。我选择的是与真实问题紧密匹配的答案,并且没有发现上述稍有误导性的误解。谢谢大家的帮助和时间!

谢谢

JP


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

解决方案



  header(Content-Type:text / plain); 

您的输出可能以HTML格式发送,其中的换行符被截断为一个空格,显示换行符,您需要< br /> 标记。


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.

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

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?

EDIT 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 ).

EDIT 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

EDIT 3:

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

$ 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).

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

解决方案

Yes, you forgot to set the Content Type of your output to 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.

这篇关于通过PHP呈现纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆