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

$skuList = explode('\n\r', $_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'])));

其他回答

PHP_EOL表面上用于以跨平台兼容的方式查找换行符,因此它可以处理DOS/Unix问题。

试试这个:

$myString = "Prepare yourself to be caught
You in the hood gettin' shot
We going throw hell of blows
got my whole frame froze";

$myArray = explode(PHP_EOL, $myString);

print_r($myArray);

这个PHP函数用换行符拆分字符串 注意:Windows中的新行是\r\n, Linux和Unix中的新行是\n 这个函数将所有新行更改为Linux模式,然后将其拆分。注意,空行将被忽略

function splitNewLine($text) {
    $code=preg_replace('/\n$/','',preg_replace('/^\n/','',preg_replace('/[\r\n]+/',"\n",$text)));
    return explode("\n",$code);
}

例子

$a="\r\n\r\n\n\n\r\rsalam\r\nman khobam\rto chi\n\rche khabar\n\r\n\n\r\r\n\nbashe baba raftam\r\n\r\n\r\n\r\n";
print_r( splitNewLine($a) );

输出

Array
(
    [0] => salam
    [1] => man khobam
    [2] => to chi
    [3] => che khabar
    [4] => bashe baba raftam
)

看起来很简单

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

你只需要传递准确的文本“\n”,直接写“\n”被用作转义序列。输入"\\"传递一个简单的反斜杠然后输入"n"

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

$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'])));

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

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