Jump to content

Displaying a portion of a file to a text area


JasonGP58

Recommended Posts

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>';

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.

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?

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>';
?>

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.