php 使用php显示.txt文件的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/928174/
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
display contents of .txt file using php
提问by vache
using this code
使用此代码
<?php
foreach (glob("*.txt") as $filename) {
$file = $filename;
$contents = file($file);
$string = implode($contents);
echo $string;
echo "<br></br>";
}
?>
i can display the contants of any txt file in the folder the problem is all the formating and so on from the txt file is skipped
我可以显示文件夹中任何 txt 文件的内容问题是所有格式等都从 txt 文件中跳过
the txt file looks like
txt文件看起来像
#nipponsei @ irc.rizon.net presents:
Title: Ah My Goddess Sorezore no Tsubasa Original Soundrack
Street Release Date: July 28, 2006
------------------------------------
Tracklist:
1. Shiawase no Iro On Air Ver
2. Peorth
3. Anata ni Sachiare
4. Trouble Chase
5. Morisato Ka no Nichijou
6. Flying Broom
7. Megami no Pride
8. Panic Station
9. Akuryou Harai
10. Hore Kusuri
11. Majin Urd
12. Hild
13. Eiichi Soudatsusen
14. Goddess Speed
15. Kaze no Deau Basho
16. Ichinan Satte, Mata...
17. Eyecatch B
18. Odayaka na Gogo
19. Heibon na Shiawase
20. Kedarui Habanera
21. Troubadour
22. Awate nai de
23. Ninja Master
24. Shinobi no Okite
25. Skuld no Hatsukoi
26. Kanashimi no Yokan
27. Kousaku Suru Ishi
28. Dai Makai Chou Kourin
29. Subete no Omoi wo Mune ni
30. Invisible Shield
31. Sparkling Battle
32. Sorezore no Tsubasa
33. Yume no Ato ni
34. Bokura no Kiseki On Air Ver
------------------------------------
Someone busted in, kicked me and asked why there was no release
of it. I forgot! I'm forgetting a lot...sorry ;_;
minglong
i the result i get looks like
我得到的结果看起来像
#nipponsei @ irc.rizon.net presents: Title: Ah My Goddess Sorezore no Tsubasa Original Soundrack Street Release Date: July 28, 2006 ------------------------------------ Tracklist: 1. Shiawase no Iro On Air Ver 2. Peorth 3. Anata ni Sachiare 4. Trouble Chase 5. Morisato Ka no Nichijou 6. Flying Broom 7. Megami no Pride 8. Panic Station 9. Akuryou Harai 10. Hore Kusuri 11. Majin Urd 12. Hild 13. Eiichi Soudatsusen 14. Goddess Speed 15. Kaze no Deau Basho 16. Ichinan Satte, Mata... 17. Eyecatch B 18. Odayaka na Gogo 19. Heibon na Shiawase 20. Kedarui Habanera 21. Troubadour 22. Awate nai de 23. Ninja Master 24. Shinobi no Okite 25. Skuld no Hatsukoi 26. Kanashimi no Yokan 27. Kousaku Suru Ishi 28. Dai Makai Chou Kourin 29. Subete no Omoi wo Mune ni 30. Invisible Shield 31. Sparkling Battle 32. Sorezore no Tsubasa 33. Yume no Ato ni 34. Bokura no Kiseki On Air Ver ------------------------------------ Someone busted in, kicked me and asked why there was no release of it. I forgot! I'm forgetting a lot...sorry ;_; minglong
回答by Peter Stuifzand
The implodedefaults to an empty string. You should call implodesomething like this:
该implode默认为空字符串。你应该这样称呼implode:
$string = implode("<br>", $contents);
回答by Gumbo
You have to add HTML line break elements to the physical line breaks. You could use the nl2brfunctionto do that:
您必须向物理换行符添加 HTML 换行符元素。您可以使用该nl2br功能来做到这一点:
foreach (glob("*.txt") as $filename) {
echo nl2br(file_get_contents($filename));
echo "<br></br>";
}
Additionally I would use the file_get_contentsfunctionrather than the combination of fileand implode.
此外,我将使用file_get_contents功能,而不是组合file和implode。
回答by cletus
If this isn't part of an HTML document, you need to change the content type:
如果这不是 HTML 文档的一部分,则需要更改内容类型:
<?php
header("Content-Type: text/plain");
foreach (glob("*.txt") as $filename) {
readfile($filename);
}
?>
If it is part of an HTML document, just do this:
如果它是 HTML 文档的一部分,只需执行以下操作:
<pre>
<?php
foreach (glob("*.txt") as $filename) {
readfile($filename);
}
?>
</pre>
Alternatively you can replace newlines with breaks:
或者,您可以用换行符替换换行符:
<?php
foreach (glob("*.txt") as $filename) {
$str = file_get_contents($filename);
echo preg_replace('!\r?\n!', '<br>', $str);
}
?>
回答by Michael Koval
As several of the other responses mentioned, it greatly depends upon the page in which you're displaying the output.
正如提到的其他几个响应,它在很大程度上取决于您显示输出的页面。
Raw Text Output
原始文本输出
If you're not adding any other content or HTML to the page. Simply change the HTTP Content-Type header to "text/plain"; that is:
如果您没有向页面添加任何其他内容或 HTML。只需将 HTTP Content-Type 标头更改为“text/plain”;那是:
header('Content-Type: text/plain');
echo file_get_contents('path/to/file');
As always, HTTP headers must be sent beforeany content is sent to the browser.
与往常一样,必须在将任何内容发送到浏览器之前发送 HTTP 标头。
(X)HTML Output
(X)HTML 输出
Replacing \n's with <br/>will notfix whitespace truncation issues; that is, the removal of adjacent spaces and/or tabs. The easiest way to get around this, also as previously mentioned, is to use the <pre>tag to enclose the contents of the file. Unfortunately, this is not enough to satisfy XHTML. There are a number of symbols that are invalid in XML unless they are properly escaped, notably including: &, <, and >.
更换\n“带S<br/>将无法修复的空白截断的问题; 也就是说,删除相邻的空格和/或制表符。解决此问题的最简单方法(也如前所述)是使用<pre>标记来包含文件的内容。不幸的是,这还不足以满足 XHTML。有一些是在XML无效,除非它们被正确转义的符号,特别是包括:&,<,和>。
Thankfully, this is also an easy fix using the str_replacemethod:
值得庆幸的是,这也是使用该str_replace方法的一个简单修复:
$raw = file_get_contents('path/to/file');
echo '<pre>';
echo str_replace($raw, array('>','<','&','%'), array('>','<','&','%'));
echo '</pre>';
回答by garyLynch
embed the text file content between
<pre></pre>tags
在<pre></pre>标签之间嵌入文本文件内容
回答by Dheeraj Sachan
write your text in a .txt file and redirect to url corresponding to that file
将您的文本写入 .txt 文件并重定向到与该文件对应的 url
php example code
php示例代码
contents of allow.txt are
allow.txt 的内容是
Authorized=True
Duration=1
OutputAnalog=NO_PLAYBACK
OutputDigital=NO_PLAYBACK
contents of deny.txt are
deny.txt 的内容是
Authorized=False
Duration=0
OutputAnalog=NO_PLAYBACK
OutputDigital=NO_PLAYBACK
contents of php file
php文件的内容
<?php
$user = $_REQUEST['username'];
$pass = $_REQUEST['password'];
$contentId = $_REQUEST['contentId'];
ob_start(); // ensures anything dumped out will be caught
// do stuff here
allowUrl = 'http://localhost/allow.txt'; // this can be set based on whatever
$denyUrl = 'http://localhost/deny.txt';
// clear out the output buffer
while (ob_get_status())
{
ob_end_clean();
}
// no redirect
if($user == "xyz" && $pass == "xyz")
header( "Location: $allowUrl" );
else
header("Location: $denyUrl");
?>
回答by Josh Anderson
Or you could just put it into a textarea like this:
或者您可以将其放入这样的 textarea 中:
<?
$file = 'file.txt';
$contents = file($file);
$string = implode("",$contents);
echo '<textarea readonly style="width:100%; height:200px;">';
echo $string;
echo "</textarea><br></br>";
?>
But only if you can and it turns out right.
但前提是你可以而且结果是正确的。
回答by truppo
file()returns an array with the lines of the file.
file()返回一个包含文件行的数组。
If you implode those without glue there will be no linebreaks at all.
如果你在没有胶水的情况下内爆那些,根本不会有换行符。
So, either get the contents unmodified using file_get_contents()(which gives you a string), or glue the implode with newline or
因此,要么使用file_get_contents()(它为您提供一个字符串)获取未修改的内容,要么用换行符粘合内爆或
回答by Sarah Vessels
Peter Stuifzand had the right idea, passing a second argument to the implode function, so I won't address that. What I will point out is that your own echo "<br></br>";code does not produce valid HTML. If you're doing HTML and want 2 line breaks, do echo "<br><br>";and if you're doing XHTML and want 2 line breaks, do echo "<br/><br/>";. Otherwise, if you only want 1 line break, the HTML br tag does not have a closing tag, so </br>is not necessary in either case.
Peter Stuifzand 有正确的想法,将第二个参数传递给 implode 函数,所以我不会解决这个问题。我要指出的是,您自己的echo "<br></br>";代码不会生成有效的 HTML。如果你在做 HTML 并且想要 2 个换行符,那么做echo "<br><br>";,如果你在做 XHTML 并且想要 2 个换行符,那么做echo "<br/><br/>";. 否则,如果您只需要 1 个换行符,则 HTML br 标签没有结束标签,因此</br>在任何一种情况下都不需要。

