views:

529

answers:

5

Hi All!

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

A: 
$lines = explode("\n", $text);
Coronatus
+4  A: 

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
i don't need to split **TEXT:** this is just an example for delimit each line!!!!!!
aSeptik
Ok, so, which character exactly delimits the lines?
Milan Babuškov
the colon! before the text!
aSeptik
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!
aSeptik
+1  A: 

file() reads a file into an array.

powtac
+3  A: 

"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
thank's also to you for the hint! +1 ;-)
aSeptik
A: 

Well, you keep editing your question so I have no idea if this will be valid by the time I press post but you can try something like this:

$text = ("TEXT1:some normal text
TEXT2:some long text here, and so on... sometimes 
i'm breaking down and...
TEXT3:some normal text
TEXT4:some normal text");

$parts = preg_split('/TEXT[0-9]+:/', $text);
array_shift($parts);

foreach ($parts as &$part) {
    $part = str_replace("\n", '', $part);
}

print_r($parts);

/*
output:
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
)
webbiedave
i'm sorry for editing my answer really! ;-( but is for make it more clear! what i want is not to split at **TEXT:** if you read the last edit you understand better! thanks for the time!
aSeptik
But you said TEXT#: delimits the lines. Did you try my code?
webbiedave
Again thanks for the time! but try to understand that TEXT is for make clear where the line start and where it finish (in the real app there is no TEXT)! now, since i'm splitting at **\n** the regex cut off also **sometimes i'm breaking down and...** cause it consider it as if it is another line!
aSeptik