Windows 和 Linux 服务器中出现爆炸错误 \r\n 和 \n
我使用爆炸函数将文本区域的包含内容放入基于行的数组中。当我在本地主机(WAMPserver 2.1)中运行此代码时,它与此代码完美配合:
$arr=explode("\r\n",$getdata);
当我上传到我的linux服务器时,我每次都需要将上面的代码更改为:
$arr=explode("\n",$getdata);
对我来说永久的解决方案是什么。哪种通用代码适用于我的两台服务器?
谢谢
I have used explode function to get textarea's contain into array based on line. When I run this code in my localhost (WAMPserver 2.1) It work perfectly with this code :
$arr=explode("\r\n",$getdata);
When I upload to my linux server I need to change above code everytime into :
$arr=explode("\n",$getdata);
What will be the permanent solution to me. Which common code will work for me for both server?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
常量 PHP_EOL 包含与平台相关的换行符,因此您可以尝试以下操作:
但更好的是规范化文本,因为您永远不知道访问者使用什么操作系统。这是标准化为仅使用 \n 作为换行符的一种方法(但也请参阅 Alex 的答案,因为他的正则表达式将处理所有类型的换行符):
The constant PHP_EOL contains the platform-dependent linefeed, so you can try this:
But even better is to normalize the text, because you never know what OS your visitors uses. This is one way to normalize to only use \n as linefeed (but also see Alex's answer, since his regex will handle all types of linefeeds):
据我所知,按换行符分割字符串的最佳方法是
preg_split
和 < a href="http://www.pcre.org/pcre.txt" rel="noreferrer">\R
:\R
匹配任何 Unicode换行序列,即不仅是LF
、CR
、CRLF
,还包括更奇特的序列,如VT
、FF
、NEL
、LS
和PS
。如果不需要这种行为(为什么?),您可以指定
BSR_ANYCRLF
选项:这将仅匹配“经典”换行序列。
As far as I know the best way to split a string by newlines is
preg_split
and\R
:\R
matches any Unicode Newline Sequence, i.e. not onlyLF
,CR
,CRLF
, but also more exotic ones likeVT
,FF
,NEL
,LS
andPS
.If that behavior isn't wanted (why?), you could specify the
BSR_ANYCRLF
option:This will match the "classic" newline sequences only.
好吧,最好的方法是将输入数据规范化为仅使用
\n
,如下所示:因为:
\n
。\r\n
。\r
。尽管如此,通过
\n
进行爆炸应该会得到最好的结果(如果你没有标准化)。Well, the best approach would be to normalize your input data to just use
\n
, like this:Since:
\n
.\r\n
.\r
.Nonetheless, exploding by
\n
should get you the best results (if you don't normalize).PHP_EOL 常量包含主机操作系统换行符的字符序列。
The PHP_EOL constant contains the character sequence of the host operating system's newline.
您可以使用
preg_split()
这将允许它工作,无论:You could use
preg_split()
which will allow it to work regardless: