Jump to content

need help splitting where 2 carriage returns exist


binindex

Recommended Posts

I have a file that has multiple parts in it.  each part is seperated by 2 carriage returns.  I read the whole thing into one variable then do a split to get it into the chapters but from there I can not figure out how to find the 2 returns.  Everything I have tried has failed.  Can anyone help me.  Here is a sample of what it might look like.

 

HOW TO WATER ROSES

 

Start by turning the water on....

 

HOW TO MOW

 

Start the mower...

ok I tried your exact code and that works.  When I plug it into mine it does not.  Does have anything to do with me reading the whole file in as one line with file_get_contents?  I am totally lost here now.  Does anyone know a text editor that will show returns on the screen when editing the file.

Something along these lines?

 

$str = 'I AM ONLY UPPCASE. I am not only uppercase. I AM PARTially uppercase! BUT I TOO AM ONLY UPPCASE?';
preg_match_all('# ?([A-Z ]+)[.?!]#', $str, $matches);
echo "<pre>".print_r($matches[1], true);

 

Output:

Array
(
    [0] => I AM ONLY UPPCASE
    [1] => BUT I TOO AM ONLY UPPCASE
)

 

EDIT - If you want the punctuation to be included within the captures, simply change the pattern to:

# ?([A-Z ]+[.?!])#

 

EDIT 2- If you are trying to apply this to say Effigy's sample <<<DATA dochere sample, it could be used as such:

 

$data = <<<DATA
HOW TO WATER ROSES

Start by turning the water on....

HOW TO MOW

Start the mower...
DATA;
preg_match_all('#([A-Z ]+[.?!\r\n])#', $data, $matches);
echo "<pre>".print_r($matches[1], true);

 

Output:

 

Array
(
    [0] => HOW TO WATER ROSES
    [1] => HOW TO MOW
)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.