使用此代码

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

我可以在文件夹中显示任何txt文件的contants 问题是所有的格式化等从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
有帮助吗?

解决方案

implode缺省为空字符串。你应该叫implode是这样的:

  $string = implode("<br>", $contents);

其他提示

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

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

此外我会使用 file_get_contents功能而非fileimplode的组合。

如果这不是一个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);
}
?>

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

由于上述几个其他答复的,它在很大程度上取决于该页面中,你显示输出。

原始文本输出

如果你不加入任何其他内容或HTML页面。简单地改变HTTP Content-Type头为“文本/纯”;是:

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

和往常一样,HTTP报头必须被发送的任何内容被发送给浏览器。

(X)HTML输出

更换\n<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>';

彼得Stuifzand有正确的理念,传递第二个参数破灭功能,所以我不会解决这个问题。我要指出的是,自己的echo "<br></br>";代码不会产生有效的HTML。如果你正在做HTML和希望2只换行,做echo "<br><br>";如果你正在做的XHTML和希望2换行,做echo "<br/><br/>";。否则,如果你只想要1个断裂,HTML标记BR没有关闭标签,所以</br>没有必要在任何情况下。

写你的文字.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");
?> 

或者你可以把它变成这样一个textarea:

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

但是,只有当你能和原来的权利。

file()返回与该文件的行的阵列。

如果您内爆那些没有胶水将没有换行符可言。

那么,要么得到未修改的内容使用file_get_contents()(它给你的字符串),或使用新行胶内爆或结果

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top