TripleDES Posted July 2, 2007 Share Posted July 2, 2007 All, What's the best way to go about parsing through a file and only writing lines that come after x and stop when y is reached? Example: -- Begin X -- We are constantly trying to improve phpfreaks and these forums -- End Y -- Link to comment https://forums.phpfreaks.com/topic/58142-parsing-file-and-printing-only-certain-lines/ Share on other sites More sharing options...
corbin Posted July 2, 2007 Share Posted July 2, 2007 I would do it with a regular expression.... maybe something like: //edit: this code doesnt work... ;p The output would be the text without the begin x and end y lines. (P.S. I didn't test that, so if it doesn't work, sorry ;p) Link to comment https://forums.phpfreaks.com/topic/58142-parsing-file-and-printing-only-certain-lines/#findComment-288351 Share on other sites More sharing options...
effigy Posted July 2, 2007 Share Posted July 2, 2007 How big is/are the file/files? Link to comment https://forums.phpfreaks.com/topic/58142-parsing-file-and-printing-only-certain-lines/#findComment-288352 Share on other sites More sharing options...
corbin Posted July 2, 2007 Share Posted July 2, 2007 Ahh found a regular expression that works! <?php $content = "-- Begin X -- We are constantly trying to improve phpfreaks and these forums -- End Y --"; $pattern = "/-- Begin ([a-z]+) --(.*)-- End ([a-z]+) --/si"; //$pattern = "/\-\- Begin ([a-z]+) \-\-(.*) \-\- End ([a-z]+) \-\-/si"; preg_match($pattern, $content, $matches); //echo $matches[1]; echo '<pre>'; print_r($matches); echo '</pre>'; /* Output: Array ( [0] => -- Begin X -- We are constantly trying to improve phpfreaks and these forums -- End Y -- [1] => X [2] => We are constantly trying to improve phpfreaks and these forums [3] => Y ) */ ?> Link to comment https://forums.phpfreaks.com/topic/58142-parsing-file-and-printing-only-certain-lines/#findComment-288356 Share on other sites More sharing options...
TripleDES Posted July 3, 2007 Author Share Posted July 3, 2007 $pattern = "/-- Begin ([a-z]+) --(.*)-- End ([a-z]+) --/si"; Wow...that regex is too complicated for me to understand. Please explain it to me so I can learn. The file is not very big. It's simply a mime encoded email that I'm trying to parse out the message body (text) so I can forward it via SMS. If there's a better way to go about it, please enlighten me. X = --mimepart_...... body...text...etc.... Y = --- Link to comment https://forums.phpfreaks.com/topic/58142-parsing-file-and-printing-only-certain-lines/#findComment-288429 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.