PHP preg_plit() 有换行但防止多行跳转

PHP preg_plit() with line breaks but prevent multi line jumps

我想 preg_split() 每一个新行一个字符串,只包含内容的行(没有多行跳转)

我试过了(\S*)\n|$

例如:

last_name, first_name

bjorge, philip
kardashian, kim
mercury, freddie

我的preg_split

(\S*)\n|$
array(2
0   =>  last_name, first_name
1   =>  
)
array(2
0   =>  
1   =>  
)
array(2
0   =>  bjorge, philip
1   =>  
)
array(2
0   =>  kardashian, kim
1   =>  
)
array(2
0   =>  mercury, freddie
1   =>  
)

我想要的数组:

array(2
0   =>  last_name, first_name
)
array(2
0   =>  bjorge, philip
)
array(2
0   =>  kardashian, kim 
)
array(2
0   =>  mercury, freddie    
)

\R+ 上拆分,这意味着 1 个或多个任何类型的换行符。

根据评论编辑:

为了不捕获仅包含空格的行,请添加 \h+(即 1 个或多个水平空格):

$string = "last_name, first_name

bjorge, philip
kardashian, kim
mercury, freddie
";
$res = preg_split('/\h*\R+/', $string, -1, PREG_SPLIT_NO_EMPTY);
print_r($res);

输出:

Array
(
    [0] => last_name, first_name
    [1] => bjorge, philip
    [2] => kardashian, kim
    [3] => mercury, freddie
)