简单,是吧?嗯,这是行不通的:-\

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

当前回答

试试“\n\r”(双引号)或者“\n”。

如果您不确定您拥有哪种类型的EOL,在爆炸之前运行str_replace,将“\n\r”替换为“\n”。

其他回答

如果内容可能在系统外部生成,那么系统使用什么作为换行符并不重要。

在收到所有这些答案后,我很惊讶,没有人简单地建议使用\R转义序列。只有一种方法可以让我考虑在自己的项目中实现它。\R提供了最简洁和直接的方法。

https://www.php.net/manual/en/regexp.reference.escape.php # ~:短信=线% 20break: 20matches % 20 \ n % 20, r 20and % 20 \ r \ n

代码:(演示)

$text = "one\ntwo\r\nthree\rfour\r\n\nfive";

var_export(preg_split('~\R~', $text));

输出:

array (
  0 => 'one',
  1 => 'two',
  2 => 'three',
  3 => 'four',
  4 => '',
  5 => 'five',
)

你试过用双引号吗?

试试这个:

explode(PHP_EOF, $lines);

虽然不完美,但我认为这是最安全的。添加nl2br:

$skuList = explode('<br />', nl2br($_POST['skuList']));

这个方法对我来说总是有效的:

$uniquepattern="@#$;?:~#abcz"//Any set of characters which you dont expect to be present in user input $_POST['skuList'] better use atleast 32 charecters.
$skuList=explode($uniquepattern,str_replace("\r","",str_replace("\n",$uniquepattern,$_POST['skuList'])));