JasonGP58 Posted October 3, 2008 Share Posted October 3, 2008 I'm brand new to php. I was wondering if anyone had some advice on the best way to grab a portion of a file from "//startHere" to "//endHere". Any forums, tutorials, or general advice would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted October 3, 2008 Share Posted October 3, 2008 This works for me, give it a try: $string = 'This is text above //startHere This is text inside //endHere this is text below.'; $string = preg_replace("~\n~",' ',$string); if(preg_match('~//startHere(.+?)//endHere~i',$string,$matches)){ $myText = $matches[1]; }else{ $myText = ''; } echo '<textarea>'.$myText.'</textarea>'; Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 3, 2008 Share Posted October 3, 2008 LittleGuy, that won't work, because you forgot the s modifier so that the . will actually match the newlines: ~//startHere(.+?)//endHere~is Try that in preg_match. Quote Link to comment Share on other sites More sharing options...
JasonGP58 Posted October 3, 2008 Author Share Posted October 3, 2008 Will that print: //start here This is text inside because that's my goal. Sorry that I wasn't clear enough on the first post. I need to include the 1st comment and exclude the second. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 3, 2008 Share Posted October 3, 2008 Change Little Guy's code to: $string = 'This is text above //startHere This is text inside //endHere this is text below.'; if(preg_match('~//startHere(.+?)//endHere~is',$string,$matches)){ $myText = '//startHere' . "\n{$matches[1]}"; }else{ $myText = ''; } echo '<textarea>'.$myText.'</textarea>'; And sorry, Little Guy, I didn't see that preg_replace that you had on top. That's not how it should be done anyway, you should use the s modifier. Quote Link to comment Share on other sites More sharing options...
JasonGP58 Posted October 6, 2008 Author Share Posted October 6, 2008 I'm pulling the data from a file, so would I be able to use pregmatch. I wouldn't want to read the entire file, then use a preg match, because that'd be a huge waste of space and time. Could I not use file_get_contents then somehow set its parameters up with fseek? Quote Link to comment Share on other sites More sharing options...
ibechane Posted October 6, 2008 Share Posted October 6, 2008 If you are reading in line by line, you can skip the preg_match and just search for the delimiters (assuming they are on separate lines). Try this (haven't tested it). <?php $resource = fopen('/file/location','r') or die('could not open file'); $output = ''; while(!feof($resource)) { $newline = fgets($resource); if($newline == '//starthere') { while(!feof($resource) AND $newline!='//endhere') { $output .= $newline; $newline = fgets($resource); } break; } } echo '<textarea>'.$output.'</textarea>'; ?> Quote Link to comment 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.