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

时间:2010-04-26 19:41:07

标签: php regex preg-split line-breaks

编辑:

需要有关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
        )

好的,现在使用

preg_split( '#\n(?!s)#' ,  $text );

我得到了

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

我想得到这个:

[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
        )

正则表达式可以获得整行,也可以在换行时拆分!

5 个答案:

答案 0 :(得分:21)

“换行符”定义不明确。 Windows仅使用CR + LF(\ r \ n),Linux LF(\ n),OSX CR(\ r \ n)。

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

preg_match('/^\R$/', "\r\n"); // 1

答案 1 :(得分: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);

结果:

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
)

答案 2 :(得分:2)

file()将文件读入数组。

答案 3 :(得分:0)

如果将数组拆分为:character ..

print_r(preg_split('/:/', $input));

答案 4 :(得分:-2)

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