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

时间:2021-08-14 22:08:40

EDITED:

need help on split Array

需要有关split array的帮助

array example:

 array (

           [0] =>
            :some normal text
            :some long text here, and so on... sometimes 
            i'm breaking down and...
            :some normal text
            :some normal text
        )

ok, now by using

好的,现在通过使用

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

i get

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

I want get this:

我想得到这个:

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

what Regex can get the entire line and also split at line break!?

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

5 个解决方案

#1


7  

Here's an example that works, even if you have a colon character embedded inside the string (but not at start of the line):

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

$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);

result:

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


21  

"line break" is ill-defined. Windows uses CR+LF (\r\n), Linux LF (\n), OSX CR (\r) only.

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

There is a little-known special character \R in preg_* regular exceptions that matches all three:

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

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

#3


2  

file() reads a file into an array.

file()将文件读入数组。

#4


0  

If you split the array on the : character..

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

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

#5


-2  

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

#1


7  

Here's an example that works, even if you have a colon character embedded inside the string (but not at start of the line):

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

$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);

result:

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


21  

"line break" is ill-defined. Windows uses CR+LF (\r\n), Linux LF (\n), OSX CR (\r) only.

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

There is a little-known special character \R in preg_* regular exceptions that matches all three:

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

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

#3


2  

file() reads a file into an array.

file()将文件读入数组。

#4


0  

If you split the array on the : character..

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

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

#5


-2  

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