我应该使用\ r或\ n来爆炸()PHP中文件的内容吗?

时间:2022-09-10 22:44:35

I'm creating a function which can accept a string which is either retrieved through file_get_contents() on a local text file, or something fetched from a url such as http://site.com/338383.txt.

我正在创建一个函数,它可以接受一个字符串,该字符串可以通过本地文本文件上的file_get_contents()检索,也可以从URL(例如http://site.com/338383.txt)获取。

The file will be a tab seperated file, with each item in the file being on its own line. Is it better to use \n or \r to explode() the string and get an array of each line?

该文件将是一个选项卡分隔文件,文件中的每个项目都在其自己的行上。使用\ n或\ r来爆炸()字符串并获取每行的数组是否更好?

I've noticed that in some cases \n doesn't work. I'd like a consistent way which works all the time. Any thoughts?

我注意到在某些情况下\ n不起作用。我想要一贯有效的方法。有什么想法吗?

4 个解决方案

#1


You can use file() to get the contents of the file as array with individual lines.

您可以使用file()将文件的内容作为具有单独行的数组。

#2


As duckyflip points out, you can use the file() function to get an array of file lines. However, if you still need to explode (for an unknown reason), you should use the PHP constant PHP_EOL instead of '\n' as this is cross-platform compliant.

正如duckyflip指出的那样,你可以使用file()函数来获取文件行数组。但是,如果您仍然需要爆炸(由于未知原因),您应该使用PHP常量PHP_EOL而不是'\ n',因为这是跨平台兼容的。

#3


Problem is that newline is defined differently for different "text/plain" encodings and platforms. The quick-and-dirty solution would probably be to use split and the regular expression "\r\n|\r|\n", however, it may break on some unicode files and it has no sense of "context". I.e. if you have a file where LF (\n) is used as a EOL marker, and there's some CRs there which should have been preserved, the CRs will be split on as well.

问题是对于不同的“文本/普通”编码和平台,换行符的定义不同。快速和肮脏的解决方案可能是使用split和正则表达式“\ r \ n | \ r | \ n”,但是,它可能会破坏某些unicode文件,并且它没有“上下文”的意义。即如果你有一个文件,其中LF(\ n)被用作EOL标记,并且那里应该保留了一些CR,那么CR也将被拆分。

#4


You can use preg_split () to explode by /\n\r|\n|\r/, and then trim () each item to make sure no trailing whitespace is remaining (if it’s appropriate).

你可以使用preg_split()来爆炸/ \ n \ r | \ n | \ r /,然后修剪()每个项目,以确保没有剩余的尾随空格(如果合适)。

#1


You can use file() to get the contents of the file as array with individual lines.

您可以使用file()将文件的内容作为具有单独行的数组。

#2


As duckyflip points out, you can use the file() function to get an array of file lines. However, if you still need to explode (for an unknown reason), you should use the PHP constant PHP_EOL instead of '\n' as this is cross-platform compliant.

正如duckyflip指出的那样,你可以使用file()函数来获取文件行数组。但是,如果您仍然需要爆炸(由于未知原因),您应该使用PHP常量PHP_EOL而不是'\ n',因为这是跨平台兼容的。

#3


Problem is that newline is defined differently for different "text/plain" encodings and platforms. The quick-and-dirty solution would probably be to use split and the regular expression "\r\n|\r|\n", however, it may break on some unicode files and it has no sense of "context". I.e. if you have a file where LF (\n) is used as a EOL marker, and there's some CRs there which should have been preserved, the CRs will be split on as well.

问题是对于不同的“文本/普通”编码和平台,换行符的定义不同。快速和肮脏的解决方案可能是使用split和正则表达式“\ r \ n | \ r | \ n”,但是,它可能会破坏某些unicode文件,并且它没有“上下文”的意义。即如果你有一个文件,其中LF(\ n)被用作EOL标记,并且那里应该保留了一些CR,那么CR也将被拆分。

#4


You can use preg_split () to explode by /\n\r|\n|\r/, and then trim () each item to make sure no trailing whitespace is remaining (if it’s appropriate).

你可以使用preg_split()来爆炸/ \ n \ r | \ n | \ r /,然后修剪()每个项目,以确保没有剩余的尾随空格(如果合适)。