php显示txt文件,使用php显示.txt文件的内容

using this code

使用这个代码

foreach (glob("*.txt") as $filename) {

$file = $filename;

$contents = file($file);

$string = implode($contents);

echo $string;

echo "
";

}

?>

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

9 个解决方案

#1

9

The implode defaults to an empty string. You should call implode something like this:

内爆默认为空字符串。你应该把它叫做内爆:

$string = implode("
", $contents);

#2

8

You have to add HTML line break elements to the physical line breaks. You could use the nl2br function to do that:

必须向物理换行符中添加HTML换行元素。你可以用nl2br函数来做:

foreach (glob("*.txt") as $filename) {

echo nl2br(file_get_contents($filename));

echo "
";

}

Additionally I would use the file_get_contents function rather than the combination of file and implode.

此外,我将使用file_get_contents函数,而不是文件和内爆的组合。

#3

4

If this isn't part of an HTML document, you need to change the content type:

如果这不是HTML文档的一部分,您需要更改内容类型:

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

foreach (glob("*.txt") as $filename) {

readfile($filename);

}

?>

If it is part of an HTML document, just do this:

如果它是HTML文档的一部分,就这样做:

 
 

foreach (glob("*.txt") as $filename) {

readfile($filename);

}

?>

Alternatively you can replace newlines with breaks:

也可以用换行符替换换行符:

foreach (glob("*.txt") as $filename) {

$str = file_get_contents($filename);

echo preg_replace('!\r?\n!', '
', $str);

}

?>

#4

1

embed the text file content between

 tags 
 

标记之间嵌入文本文件内容

#5

1

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内容类型头更改为“text/plain”;那就是:

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

echo file_get_contents('path/to/file');

As always, HTTP headers must be sent before any content is sent to the browser.

与往常一样,在将任何内容发送到浏览器之前,必须发送HTTP报头。

(X)HTML Output

Replacing \n's with
will not fix 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

 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: &, .

替换\n将不会修复空格截断问题;也就是说,删除相邻的空格和/或制表符。解决这个问题的最简单方法是使用

标记来封装文件的内容。不幸的是,这还不足以满足XHTML。除非正确转义,否则XML中有许多符号是无效的,特别是:&、

Thankfully, this is also an easy fix using the str_replace method:

值得庆幸的是,使用str_replace方法也很容易修复:

$raw = file_get_contents('path/to/file');

echo '

';

echo str_replace($raw, array('>','

echo '

';

#6

0

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 "
"; code does not produce valid HTML. If you're doing HTML and want 2 line breaks, do echo "
"; and if you're doing XHTML and want 2 line breaks, do echo "
";. Otherwise, if you only want 1 line break, the HTML br tag does not have a closing tag, so is not necessary in either case.

Peter Stuifzand的想法是对的,他给内爆函数传递了第二个参数,所以我就不讲了。我要指出的是你自己的echo "

";代码不能生成有效的HTML。如果你在做HTML,想要两条换行符,请重复“

”;如果您正在使用XHTML并想要有两个换行符,请使用echo“

”;否则,如果您只想要一个换行符,那么HTML br标记没有结束标记,因此在任何一种情况下都不需要

#7

0

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

允许的内容。三是

Authorized=True

Duration=1

OutputAnalog=NO_PLAYBACK

OutputDigital=NO_PLAYBACK

contents of deny.txt are

的内容予以否认。三是

Authorized=False

Duration=0

OutputAnalog=NO_PLAYBACK

OutputDigital=NO_PLAYBACK

contents of php file

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");

?>

#8

0

Or you could just put it into a textarea like this:

或者你可以把它放到这样的文本区域:

$file = 'file.txt';

$contents = file($file);

$string = implode("",$contents);

echo '';

echo $string;

echo "
";

?>

But only if you can and it turns out right.

但前提是你能,而且结果是对的。

#9

0

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()不修改内容(这将给您一个字符串),要么使用换行符或

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值