PHP REGEX - 在换行符时由preg_split发送到数组的文本

Luc*_*ofi 4 php regex line-breaks preg-split

编辑:

需要有关split array的帮助

数组示例:

 array (

           [0] =>
            :some normal text
            :some long text here, and so on... sometimes 
            i'm breaking down and...
            :some normal text
            :some normal text
        )
Run Code Online (Sandbox Code Playgroud)

好的,现在通过使用

preg_split( '#\n(?!s)#' ,  $text );
Run Code Online (Sandbox Code Playgroud)

我明白了

[0] => Array
        (
            [0] => some normal text
            [1] => some long text here, and so on... sometimes
            [2] => some normal text
            [3] => some normal text
        )
Run Code Online (Sandbox Code Playgroud)

我想得到这个:

[0] => Array
        (
            [0] => some normal text
            [1] => some long text here, and so on... sometimes i'm breaking down and...
            [2] => some normal text
            [3] => some normal text
        )
Run Code Online (Sandbox Code Playgroud)

什么正则表达式可以获得整条线,并在换行时分开!?

Tgr*_*Tgr 21

"换行"是不明确的.Windows仅使用CR + LF(\ r \n),Linux LF(\n),OSX CR(\ r \n).

在preg_*常规异常中有一个鲜为人知的特殊字符\ R,它们匹配所有三个:

preg_match('/^\R$/', "\r\n"); // 1
Run Code Online (Sandbox Code Playgroud)


Mil*_*kov 7

这是一个有效的例子,即使你在字符串中嵌入冒号字符(但不是在行的开头):

$input = ":some normal text
:some long text here, and so on... sometimes
i'm breaking: down and...
:some normal text
:some normal text";

$array = preg_split('/$\R?^:/m', $input);
print_r($array);
Run Code Online (Sandbox Code Playgroud)

结果:

Array
(
    [0] => some normal text
    [1] => some long text here, and so on... sometimes
           i'm breaking: down and...
    [2] => some normal text
    [3] => some normal text
)
Run Code Online (Sandbox Code Playgroud)