Explode PHP string by new line

2019-01-02 16:50发布

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

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

标签: php
17条回答
笑指拈花
2楼-- · 2019-01-02 17:18

try

explode(chr(10), $_POST['skuList']);
查看更多
公子世无双
3楼-- · 2019-01-02 17:18

Have you tried using double quotes?

查看更多
孤独寂梦人
4楼-- · 2019-01-02 17:18

PHP_EOL is ostensibly used to find the newline character in a cross-platform-compatible way, so it handles DOS/Unix issues.

Try this:

$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);
查看更多
一个人的天荒地老
5楼-- · 2019-01-02 17:19

This method always works for me:

$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'])));
查看更多
萌妹纸的霸气范
6楼-- · 2019-01-02 17:20

Try this:

explode(PHP_EOF, $lines);
查看更多
君临天下
7楼-- · 2019-01-02 17:23

For a new line, it's just

$list = explode("\n", $text);

For a new line and carriage return (as in Windows files), it's as you posted. Is your skuList a text area?

查看更多
登录 后发表回答