PHP上传文本文件换行问题

发布于 2024-12-18 19:53:16 字数 361 浏览 0 评论 0原文

我正在编写一个简单的脚本,该脚本将一个 txt 文件发布到其中,然后显示文件内容。但是,我在保留换行符时遇到问题。

它是用 Windows 记事本保存的,我认为它为每个换行符添加了 CR LF,然后通过 PHP 表单上传。我可以回显整个文件内容,但我需要一种方法将数据拆分为新行。

我已经尝试过执行 echo str_replace(array("\r\n", "\r", "\n"), '
', $file);
nl2br($file)。两者都不起作用。

我正在使用 `file_get_contents($_FILES['file']['tmp_name'])' 打开文件,

谢谢。

I'm working on a simple script that get's a txt file posted to it and then displays the file contents. However, I'm having issues preserving line breaks.

It's being saved with windows notepad, which I think adds the CR LF for each line break, and then uploaded through a PHP form. I can echo the whole file contents but I need a way to split the data into new lines.

I've already tried to do echo str_replace(array("\r\n", "\r", "\n"), '<br />', $file); and nl2br($file). Neither worked.

I'm opening the file with `file_get_contents($_FILES['file']['tmp_name'])'

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

つ可否回来 2024-12-25 19:53:16

您是否考虑过使用 PHP 中的 file() 函数将文件读入数组?

http://php.net/manual/en/function.file.php

然后您可以根据需要进行输出,但每一行都将位于数组的单个元素中。

Have you considered reading the file into an array using the file() function in PHP?

http://php.net/manual/en/function.file.php

Then you can output as needed, but each line will be in a single element of the array.

绾颜 2024-12-25 19:53:16

对我来说更快的方法是复制粘贴任何文本或 Excel 或 HTML 表类型或换行符类型的数据并将其粘贴到文本区域而不是 inputextbox:

 <textarea  id="txtArea" name="txtArea" rows="40" cols="170"></textarea>
 <br>
 <input type="submit" value="split lines into array" /> 

在接收文件的表单中:

 $txtArea ='';  
 $txtArea = $_POST['txtArea'];  
 $TA = $_POST['txtArea'];  
 $string = $TA;  
 $array = preg_split ('/$\R?^/m', $string); 
// or any of these: 
// $array = explode(PHP_EOL,$string);  
// $array = explode("\n", $txtArea); 
 echo "<br>A0: ".$array[0];
 echo "<br>A1: ".@$array[1];
 echo "<br>A2: ".@$array[2];

What works faster for me is to copy paste any text or Excel or HTML table type or newline type of data and paste it into a textarea instead of an inputextbox:

 <textarea  id="txtArea" name="txtArea" rows="40" cols="170"></textarea>
 <br>
 <input type="submit" value="split lines into array" /> 

in the form receiving file:

 $txtArea ='';  
 $txtArea = $_POST['txtArea'];  
 $TA = $_POST['txtArea'];  
 $string = $TA;  
 $array = preg_split ('/$\R?^/m', $string); 
// or any of these: 
// $array = explode(PHP_EOL,$string);  
// $array = explode("\n", $txtArea); 
 echo "<br>A0: ".$array[0];
 echo "<br>A1: ".@$array[1];
 echo "<br>A2: ".@$array[2];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文