Try it this way.

<?
$search_str = "something";
$file = "blah";

$found = 0;
$fp = fopen($file, "r");
while(!feof($fp) && !$found) {
        $contents = fgets($fp, 4096);
        if ( strstr($contents, $search_str) ) {
                $found = 1;
        }
}
fclose($fp);

if ( $found ) {
        echo "<pre>";
        readfile($file);
        echo "</pre>";
}       
?>

Christopher S. Cosby
SciCare Software Services

> -----Original Message-----
> From: Tim Loepp [mailto:[EMAIL PROTECTED]] 
> Sent: Friday, June 28, 2002 7:43 PM
> To: [EMAIL PROTECTED]
> Subject: [PHP] Searching for string in text file help?
> 
> 
> Hello, I'm hoping someone can help me as I' new to this and 
> can't figure it
> out myself.
> 
> PHP4.20 Win32
> 
> I'm trying to seach for a string that would occur about 10 
> lines into a text
> file. I don't want to search the entire file, and I just want 
> to know if the
> string exsists or not, but I don't really know what I'm 
> doing. This is what
> I was trying, it occurs inside a loop that is listing the files in a
> directory.
> 
>  $fp = fopen("$target_filename", "r");
>  $contents = fgets("$fp","4096");
>   if(strstr("$contents", "$search_str"))
>   {
>       echo "<pre>";
>       readfile("$target_filename");
>       echo "</pre>";
>   }
> 
> Thanks
> Tim Loepp
> 
> 
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 


     - - - - - - -  Appended by Scientific-Atlanta, Inc.  - - - - - - -  
This e-mail and any attachments may contain information which is confidential, 
proprietary, privileged or otherwise protected by law. The information is solely 
intended for the named addressee (or a person responsible for delivering it to the 
addressee). If you are not the intended recipient of this message, you are not 
authorized to read, print, retain, copy or disseminate this message or any part of it. 
If you have received this e-mail in error, please notify the sender immediately by 
return e-mail and delete it from your computer. 

Reply via email to