在PHP中输出带换行符的文本文件

时间:2022-09-13 09:49:36

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>

9 个解决方案

#1


29  

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函数。

#2


20  

One line of code:

一行代码:

 echo nl2br( file_get_contents('file.txt') );

#3


4  

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代码中显示文件的输出格式与文本文件中的格式相同,则可以使用一对预标记包装echo语句:

echo "<pre>";
echo $pageText;
echo "</pre>";

Some of the other answers look promising depending on what you are trying todo.

其他一些答案看起来很有希望取决于你正在尝试做什么。

#4


2  

Before the echo, be sure to include

在回声之前,一定要包括

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

#5


2  

For simple reads like this, I'd do something like this:

对于像这样的简单读取,我会做这样的事情:

$fileContent = file_get_contents("filename.txt");

echo str_replace("\n","&lt;br&gt;",$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.

希望这可以帮助。

#6


0  

Are you outputting to HTML or plain text? If HTML try adding a <br> at the end of each line. e.g.

您输出的是HTML还是纯文本?如果HTML尝试在每行的末尾添加一个
。例如

while (!feof($handle)) {
  $buffer = fgets($handle, 4096); // Read a line.
  echo "$buffer<br/>";
} 

#7


0  

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>&nbsp;<br>" );
echo nl2br ( $text );
?>

#8


0  

Say you have an index.php file hosted by the web server. You want to insert some multi-line text file contents into it. That's how you do it:

假设您有一个由Web服务器托管的index.php文件。您想要将一些多行文本文件内容插入其中。你就是这样做的:

<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 echo argument.

这个 部分只是一个简写,它指示Web服务器,它需要被视为PHP echo参数。

#9


-1  

You need to wrap your PHP code into <?php <YOU CODE HERE >?>, and save it as .php or .php5 (depends on your apache set up).

您需要将PHP代码包装到 ?>中,并将其保存为.php或.php5(取决于您的apache设置)。

#1


29  

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函数。

#2


20  

One line of code:

一行代码:

 echo nl2br( file_get_contents('file.txt') );

#3


4  

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代码中显示文件的输出格式与文本文件中的格式相同,则可以使用一对预标记包装echo语句:

echo "<pre>";
echo $pageText;
echo "</pre>";

Some of the other answers look promising depending on what you are trying todo.

其他一些答案看起来很有希望取决于你正在尝试做什么。

#4


2  

Before the echo, be sure to include

在回声之前,一定要包括

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

#5


2  

For simple reads like this, I'd do something like this:

对于像这样的简单读取,我会做这样的事情:

$fileContent = file_get_contents("filename.txt");

echo str_replace("\n","&lt;br&gt;",$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.

希望这可以帮助。

#6


0  

Are you outputting to HTML or plain text? If HTML try adding a <br> at the end of each line. e.g.

您输出的是HTML还是纯文本?如果HTML尝试在每行的末尾添加一个
。例如

while (!feof($handle)) {
  $buffer = fgets($handle, 4096); // Read a line.
  echo "$buffer<br/>";
} 

#7


0  

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>&nbsp;<br>" );
echo nl2br ( $text );
?>

#8


0  

Say you have an index.php file hosted by the web server. You want to insert some multi-line text file contents into it. That's how you do it:

假设您有一个由Web服务器托管的index.php文件。您想要将一些多行文本文件内容插入其中。你就是这样做的:

<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 echo argument.

这个 部分只是一个简写,它指示Web服务器,它需要被视为PHP echo参数。

#9


-1  

You need to wrap your PHP code into <?php <YOU CODE HERE >?>, and save it as .php or .php5 (depends on your apache set up).

您需要将PHP代码包装到 ?>中,并将其保存为.php或.php5(取决于您的apache设置)。