4

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!?

Luca Filosofi
  • 30,905
  • 9
  • 70
  • 77
  • 2
    Doesn't sound like you want to split on newlines at all but rather `TEXT:` – webbiedave Apr 26 '10 at 19:49
  • ok, just for make things clear, i need a REGEX that donn't cut off the line but keep it fully! all other method seen here are not for me! ;-) – Luca Filosofi Apr 26 '10 at 19:51
  • forget the TEXT# , just consider my Regex, it is working well, but it split also the long string, i need it to keep the long lines fully!!! – Luca Filosofi Apr 26 '10 at 20:08
  • ok, i have updated once more time my question, the delimiter is a **colon** **:** what is the regex i'm finding!? ;-) – Luca Filosofi Apr 26 '10 at 21:14

5 Answers5

21

"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
Tgr
  • 27,442
  • 12
  • 81
  • 118
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
)
Milan Babuškov
  • 59,775
  • 49
  • 126
  • 179
2

file() reads a file into an array.

powtac
  • 40,542
  • 28
  • 115
  • 170
  • Thanks posting something useful, non-intuitive, and answers the needs of many similar questions. Even if it ignores the stated requirements. – SamGoody Feb 20 '12 at 08:07
0

If you split the array on the : character..

print_r(preg_split('/:/', $input));
Jonathan
  • 1,542
  • 3
  • 16
  • 24
user3102529
  • 63
  • 2
  • 9
-2
$lines = explode("\n", $text);
Amy B
  • 17,874
  • 12
  • 64
  • 83