开发者

PHP REGEX - text to array by preg_split at line break

EDITED:

need help on 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!?


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

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

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


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
)


file() reads a file into an array.


If you split the array on the : character..

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜