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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.