在Windows和Linux服务器中爆炸错误\ r \ n和\ n [英] explode error \r\n and \n in windows and linux server

查看:261
本文介绍了在Windows和Linux服务器中爆炸错误\ r \ n和\ n的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用explode函数将textarea的包含物放入基于行的数组中.当我在本地主机(WAMPserver 2.1)中运行此代码时,它可以与以下代码完美配合:

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);

当我上传到Linux服务器时,我每次都需要将上述代码更改为:

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?

谢谢

推荐答案

常量PHP_EOL包含平台相关的换行符,因此您可以尝试以下操作:

The constant PHP_EOL contains the platform-dependent linefeed, so you can try this:

$arr = explode(PHP_EOL, $getdata);

但是更好的办法是对文本进行规范化,因为您永远不知道访问者使用什么操作系统.这是仅使用\ n作为换行符进行归一化的一种方法(但也请参见Alex的回答,因为他的正则表达式将处理所有类型的换行符):

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):

$getdata = str_replace("\r\n", "\n", $getdata);
$arr = explode("\n", $getdata);

这篇关于在Windows和Linux服务器中爆炸错误\ r \ n和\ n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆