用新行爆炸PHP字符串 [英] Explode PHP string by new line

查看:67
本文介绍了用新行爆炸PHP字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单,对吧?好吧,这不起作用:-\

Simple, right? Well, this isn't working :-\

$skuList = explode('\n\r', $_POST['skuList']);

推荐答案

最佳实践

如第一个答案的注释中所述,最佳实践是使用PHP常量 PHP_EOL ,代表当前系统的EOL(行尾).

As mentioned in the comment to the first answer, the best practice is to use the PHP constant PHP_EOL which represents the current system's EOL (End Of Line).

$skuList = explode(PHP_EOL, $_POST['skuList']);

PHP提供了许多其他非常有用的有用的常量使您的代码系统独立,请参见此链接,以找到有用且与系统无关的目录常量

PHP provides a lot of other very useful constants that you can use to make your code system independent, see this link to find useful and system independent directory constants.

警告

这些常数使您的页面系统独立,但是当将常数与存储在另一系统上的数据一起使用时,从一个系统迁移到另一个系统时可能会遇到问题.新系统的常数可能与先前系统的常数不同,并且存储的数据可能不再起作用.因此,在存储数据之前先对其进行完全解析,以删除所有与系统相关的部分.

These constants make your page system independent, but you might run into problems when moving from one system to another when you use the constants with data stored on another system. The new system's constants might be different from the previous system's and the stored data might not work anymore. So completely parse your data before storing it to remove any system dependent parts.

更新

Andreas的评论使我意识到,此处介绍的最佳实践"解决方案不适用于所描述的用例:服务器的EOL(PHP)与浏览器(任何操作系统)的EOL没有任何关系.正在使用,但是(浏览器)是字符串的来源.

Andreas' comment made me realize that the 'Best Practice' solution I present here does not apply to the described use-case: the server's EOL (PHP) does not have anything to do with the EOL the browser (any OS) is using, but that (the browser) is where the string is coming from.

因此,请使用@Alin_Purcaru(三下)中的解决方案来涵盖您的所有基础(并支持他的回答) :

So please use the solution from @Alin_Purcaru (three down) to cover all your bases (and upvote his answer):

$skuList = preg_split('/\r\n|\r|\n/', $_POST['skuList']);

这篇关于用新行爆炸PHP字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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