在 PHP 中输出带有换行符的文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2059740/
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
Output text file with line breaks in PHP
提问by usertest
I'm trying to open a text file and output its contents with the code below. The text file includes line breaks but when I echo the file its unformatted. How do I fix this?
我正在尝试打开一个文本文件并使用以下代码输出其内容。文本文件包含换行符,但是当我回显文件时,它是未格式化的。我该如何解决?
Thanks.
谢谢。
<html>
<head>
</head>
<body>
$fh = fopen("filename.txt", 'r');
$pageText = fread($fh, 25000);
echo $pageText;
</body>
</html>
回答by MattBelanger
To convert the plain text line breaks to html line breaks, try this:
要将纯文本换行符转换为 html 换行符,请尝试以下操作:
$fh = fopen("filename.txt", 'r');
$pageText = fread($fh, 25000);
echo nl2br($pageText);
Note the nl2br function wrapping the text.
请注意包装文本的 nl2br 函数。
回答by Gordon
One line of code:
一行代码:
echo nl2br( file_get_contents('file.txt') );
回答by Jeremy Heslop
If you just want to show the output of the file within the html code formatted the same way it is in the text file you can wrap your echo statement with a pair of pre tags:
如果您只想在 html 代码中显示文件的输出,格式与文本文件中的格式相同,您可以使用一对 pre 标签包装您的 echo 语句:
echo "<pre>";
echo $pageText;
echo "</pre>";
Some of the other answers look promising depending on what you are trying todo.
根据您尝试做的事情,其他一些答案看起来很有希望。
回答by brianary
Before the echo, be sure to include
在回声之前,一定要包括
header('Content-Type: text/plain');
回答by bng44270
For simple reads like this, I'd do something like this:
对于像这样的简单读取,我会做这样的事情:
$fileContent = file_get_contents("filename.txt");
echo str_replace("\n","<br>",$fileContent);
This will take care of carriage return and output the text. Unless I'm writing to a file, I don't use fopen and related functions.
这将处理回车并输出文本。除非我正在写入文件,否则我不使用 fopen 和相关函数。
Hope this helps.
希望这可以帮助。
回答by Robin Summerhill
Are you outputting to HTML or plain text? If HTML try adding a <br>at the end of each line. e.g.
您是输出为 HTML 还是纯文本?如果 HTML 尝试<br>在每行的末尾添加一个。例如
while (!feof($handle)) {
$buffer = fgets($handle, 4096); // Read a line.
echo "$buffer<br/>";
}
回答by chantal
Trying to get line breaks to work reading a .txt file on Apache2 and PHP 5.3.3 with MacOSX 10.6.6 and Camino, the echo nl2br( $text);didn't work right until I printed the file size first too. BTW it doesn't seem to matter if the .txt file has Linux/MacOSX LF or Windows CRLF line breaks or the text encoding is UTF-8 or Windows Latin1, Camino gets it out OK.
试图让换行符在 MacOSX 10.6.6 和 Camino 上读取 Apache2 和 PHP 5.3.3 上的 .txt 文件,echo nl2br( $text); 直到我也先打印文件大小才正常工作。顺便说一句,.txt 文件是否具有 Linux/MacOSX LF 或 Windows CRLF 换行符或文本编码是 UTF-8 或 Windows Latin1 似乎并不重要,Camino 将其搞定。
<?php
$filename = "/Users/Shared/Copies/refrain.txt";
$file_ptr = fopen ( $filename, "r" );
$file_size = filesize ( $filename );
$text = fread ( $file_ptr, $file_size );
fclose ( $file_ptr );
echo ( "File size : $file_size bytes<br> <br>" );
echo nl2br ( $text );
?>
回答by Neurotransmitter
Say you have an index.phpfile hosted by the web server. You want to insert some multi-line text file contents into it. That's how you do it:
假设您有一个index.php由 Web 服务器托管的文件。您想在其中插入一些多行文本文件内容。这就是你的做法:
<body>
<div>Some multi-line message below:</div>
<div><?= nl2br(file_get_contents('message.txt.asc')); ?></div>
</body>
This <?= ... ?>part is just a shorthand, which instructs the web server, that it needs to be treated as a PHP echoargument.
这<?= ... ?>部分只是一个速记,它指示 Web 服务器,它需要被视为 PHPecho参数。
回答by Jay Zeng
You need to wrap your PHP code into <?php <YOU CODE HERE >?>, and save it as .phpor .php5(depends on your apache set up).
您需要将 PHP 代码包装成<?php <YOU CODE HERE >?>,并将其另存为.php或.php5(取决于您的 apache 设置)。

