Author Topic: Displaying a portion of a file to a text area  (Read 730 times)

0 Members and 1 Guest are viewing this topic.

Offline JasonGP58Topic starter

  • Irregular
  • Posts: 3
    • View Profile
Displaying a portion of a file to a text area
« on: October 03, 2008, 03:02:59 PM »
I'm brand new to php.  :o

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.

Offline The Little Guy

  • Freak!
  • Posts: 6,098
  • Gender: Male
  • Jinkies!
    • View Profile
    • PHPSnips
Re: Displaying a portion of a file to a text area
« Reply #1 on: October 03, 2008, 03:22:00 PM »
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>';
phpLive - A powerful library that implements many common tasks to make php programming faster. Supports extensions and plugins. Current version: 1.0.0-Alpha
JPG to ASCII Converter | Advanced Image CAPTCHA | Simple User Login | Check If User Is Logged In
http://dreamhost.com (promo code: 8RN4)
$30 off 1 year of hosting
$40 off 2 years of hosting

Offline DarkWater

  • Freak!
  • Posts: 6,158
  • Gender: Male
    • View Profile
Re: Displaying a portion of a file to a text area
« Reply #2 on: October 03, 2008, 04:52:25 PM »
LittleGuy, that won't work, because you forgot the s modifier so that the . will actually match the newlines:

Code: [Select]
~//startHere(.+?)//endHere~is

Try that in preg_match.
Info:Apache 2.2.4 | PHP 6.0.0-dev, PHP 5.2.4 | Ubuntu 8.04 | Age: 16 | Coding PHP and CSS+(X)HTML: 5-6 years | Feel free to PM me if you need help!
Αν χρειάζεσαι ένας μεταφραστής, μπορέις να μου μιλάς.  Μιλώ καλά.
Quote from: Crayon Violent
If you ask for a banana, would you expect someone to hand you an orange? Or a fork?  No you wouldn't, because you asked for a banana, and you expect them to understand that a banana is not an orange or a fork.  It's the same principle.

Offline JasonGP58Topic starter

  • Irregular
  • Posts: 3
    • View Profile
Re: Displaying a portion of a file to a text area
« Reply #3 on: October 03, 2008, 04:56:16 PM »
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.

Offline DarkWater

  • Freak!
  • Posts: 6,158
  • Gender: Male
    • View Profile
Re: Displaying a portion of a file to a text area
« Reply #4 on: October 03, 2008, 04:58:08 PM »
Change Little Guy's code to:
Code: [Select]

$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.
Info:Apache 2.2.4 | PHP 6.0.0-dev, PHP 5.2.4 | Ubuntu 8.04 | Age: 16 | Coding PHP and CSS+(X)HTML: 5-6 years | Feel free to PM me if you need help!
Αν χρειάζεσαι ένας μεταφραστής, μπορέις να μου μιλάς.  Μιλώ καλά.
Quote from: Crayon Violent
If you ask for a banana, would you expect someone to hand you an orange? Or a fork?  No you wouldn't, because you asked for a banana, and you expect them to understand that a banana is not an orange or a fork.  It's the same principle.

Offline JasonGP58Topic starter

  • Irregular
  • Posts: 3
    • View Profile
Re: Displaying a portion of a file to a text area
« Reply #5 on: October 05, 2008, 09:47:21 PM »
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?

Offline ibechane

  • Irregular
  • Posts: 41
    • View Profile
Re: Displaying a portion of a file to a text area
« Reply #6 on: October 06, 2008, 08:36:51 AM »
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).

Code: [Select]
<?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>';
?>