PHP上传文本文件换行问题
我正在编写一个简单的脚本,该脚本将一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否考虑过使用 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.
对我来说更快的方法是复制粘贴任何文本或 Excel 或 HTML 表类型或换行符类型的数据并将其粘贴到文本区域而不是 inputextbox:
在接收文件的表单中:
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:
in the form receiving file: