binindex Posted February 25, 2009 Share Posted February 25, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/146803-need-help-splitting-where-2-carriage-returns-exist/ Share on other sites More sharing options...
effigy Posted February 25, 2009 Share Posted February 25, 2009 <pre> <?php $data = <<<DATA HOW TO WATER ROSES Start by turning the water on.... HOW TO MOW Start the mower... DATA; print_r(preg_split('/(?:\r?\n)+/', $data)); ?> </pre> Quote Link to comment https://forums.phpfreaks.com/topic/146803-need-help-splitting-where-2-carriage-returns-exist/#findComment-770966 Share on other sites More sharing options...
binindex Posted February 25, 2009 Author Share Posted February 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/146803-need-help-splitting-where-2-carriage-returns-exist/#findComment-771327 Share on other sites More sharing options...
effigy Posted February 25, 2009 Share Posted February 25, 2009 file_get_contents should be fine. Most editors do; I use Notepad++. Quote Link to comment https://forums.phpfreaks.com/topic/146803-need-help-splitting-where-2-carriage-returns-exist/#findComment-771336 Share on other sites More sharing options...
binindex Posted February 25, 2009 Author Share Posted February 25, 2009 ok I added {3} to the back of yours and I think it is working. Will test it out and let yea know. Thanks for helping me. Quote Link to comment https://forums.phpfreaks.com/topic/146803-need-help-splitting-where-2-carriage-returns-exist/#findComment-771362 Share on other sites More sharing options...
binindex Posted February 25, 2009 Author Share Posted February 25, 2009 ok does anyone know how to grab all sentences that are all caps. I know preg_match_all but can't figure out the regex for it to stop at the end. There are not '.' at the end. Quote Link to comment https://forums.phpfreaks.com/topic/146803-need-help-splitting-where-2-carriage-returns-exist/#findComment-771387 Share on other sites More sharing options...
nrg_alpha Posted February 25, 2009 Share Posted February 25, 2009 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/146803-need-help-splitting-where-2-carriage-returns-exist/#findComment-771404 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.