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

4
  • 2
    Doesn't sound like you want to split on newlines at all but rather TEXT: Commented Apr 26, 2010 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! ;-) Commented Apr 26, 2010 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!!! Commented Apr 26, 2010 at 20:08
  • ok, i have updated once more time my question, the delimiter is a colon : what is the regex i'm finding!? ;-) Commented Apr 26, 2010 at 21:14

5 Answers 5

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
Sign up to request clarification or add additional context in comments.

Comments

6

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
)

3 Comments

i don't need to split TEXT: this is just an example for delimit each line!!!!!!
Ok, so, which character exactly delimits the lines?
thank's Milan! i think the hardest thing, this time, for me, is to make my question clear! doh! %-) but finally we got it so thank's again! +1 and check!
2

file() reads a file into an array.

1 Comment

Thanks posting something useful, non-intuitive, and answers the needs of many similar questions. Even if it ignores the stated requirements.
0

If you split the array on the : character..

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

Comments

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

1 Comment

Does not handle any case except \n

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.