4

编辑:

需要拆分数组的帮助

数组示例:

 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
        )

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

4

5 回答 5

21

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

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

preg_match('/^\R$/', "\r\n"); // 1
于 2010-04-26T19:56:06.587 回答
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
)
于 2010-04-26T19:43:17.430 回答
2

file()将文件读入数组。

于 2010-04-26T19:44:14.710 回答
0

如果您在 : 字符上拆分数组..

print_r(preg_split('/:/', $input));
于 2014-12-02T20:38:52.687 回答
-2
$lines = explode("\n", $text);
于 2010-04-26T19:43:03.887 回答