5

使用此代码

<?php
foreach (glob("*.txt") as $filename) {   
    $file = $filename;
    $contents = file($file); 
    $string = implode($contents); 
    echo $string;
    echo "<br></br>";
}
?>

我可以在文件夹中显示任何 txt 文件的内容问题是所有的格式,等等从 txt 文件被跳过

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

我得到的结果看起来像

#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
4

9 回答 9

9

implode默认为空字符串。你应该implode这样称呼:

  $string = implode("<br>", $contents);
于 2009-05-29T21:13:17.753 回答
8

您必须将 HTML 换行符添加到物理换行符。您可以使用该nl2br功能来做到这一点:

foreach (glob("*.txt") as $filename) {
    echo nl2br(file_get_contents($filename));
    echo "<br></br>";
}

另外我会使用file_get_contents函数而不是fileand的组合implode

于 2009-05-29T21:13:40.933 回答
4

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

<?php
header("Content-Type: text/plain");
foreach (glob("*.txt") as $filename) { 
  readfile($filename);
}
?>

如果它是 HTML 文档的一部分,只需执行以下操作:

<pre>
<?php
foreach (glob("*.txt") as $filename) { 
  readfile($filename);
}
?>
</pre>

或者,您可以用换行符替换换行符:

<?php
foreach (glob("*.txt") as $filename) { 
  $str = file_get_contents($filename);
  echo preg_replace('!\r?\n!', '<br>', $str);
}
?>
于 2009-05-29T21:20:34.663 回答
1

<pre></pre> 在标签之间嵌入文本文件内容

于 2009-05-29T21:13:10.763 回答
1

正如提到的其他几个响应,这在很大程度上取决于您显示输出的页面。

原始文本输出

如果您不向页面添加任何其他内容或 HTML。只需将 HTTP Content-Type 标头更改为“text/plain”;那是:

header('Content-Type: text/plain');
echo file_get_contents('path/to/file');

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

(X)HTML 输出

替换\n's<br/>不会解决空格截断问题;也就是说,删除相邻的空格和/或制表符。解决这个问题的最简单方法(也如前所述)是使用<pre>标签来包含文件的内容。不幸的是,这还不足以满足 XHTML。除非正确转义,否则 XML 中有许多无效符号,特别是包括:&<>.

值得庆幸的是,这也是使用以下str_replace方法的简单修复:

$raw = file_get_contents('path/to/file');
echo '<pre>';
echo str_replace($raw, array('>','<','&','%'), array('&gt;','&lt;','&amp;','&#37;'));
echo '</pre>';
于 2009-05-31T00:38:45.897 回答
0

file()返回包含文件行的数组。

如果您将那些没有胶水的内爆,则根本不会有换行符。

因此,要么使用未修改的内容file_get_contents()(它给你一个字符串),要么用换行符粘合内爆或

于 2009-05-29T21:16:47.620 回答
0

Peter Stuifzand 的想法是正确的,将第二个参数传递给 implode 函数,所以我不会解决这个问题。我要指出的是,您自己的echo "<br></br>";代码不会生成有效的 HTML。如果您正在编写 HTML 并想要 2 个换行符,请执行echo "<br><br>";;如果您正在执行 XHTML 并想要 2 个换行符,请执行echo "<br/><br/>";. 否则,如果您只需要 1 个换行符,则 HTML br 标记没有结束标记,因此</br>在任何一种情况下都不需要。

于 2009-05-29T21:20:08.080 回答
0

将您的文本写入 .txt 文件并重定向到与该文件对应的 url

php示例代码

allow.txt 的内容是

Authorized=True
Duration=1
OutputAnalog=NO_PLAYBACK
OutputDigital=NO_PLAYBACK

deny.txt 的内容是

Authorized=False
Duration=0
OutputAnalog=NO_PLAYBACK
OutputDigital=NO_PLAYBACK

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");
?> 
于 2014-10-13T06:18:18.923 回答
0

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

<?
$file = 'file.txt';
$contents = file($file); 
$string = implode("",$contents); 
echo '<textarea readonly style="width:100%; height:200px;">';
echo $string;
echo "</textarea><br></br>";
?>

但前提是你可以并且结果是正确的。

于 2015-04-21T16:49:37.557 回答