Jump to content

Use PHP to scan txt file line by line.


Doom750

Recommended Posts

Hi. 

 

1.  I'm a complete noob at programming.  Also, unless I'm stupid, my searches for this topic were unfruitful.

 

2.  My overall goal is to have a very simple web interface to search a single text file; the text file in question has over a million lines in it (each line is like this example: 

//servername/directory/directory1/mypic.tif

 

I want to create something that will search this file and output the results to the webpage in the form of a clickable link to the folder that an item is in (or just the folder itself, if that's the case).  So, i guess the output, in html, is like this (maybe???):

<a href="file://servername\directory11\directory12">directory12</a>

 

So that way a person could go to a webpage, enter in a search term(s) and be able to see the files on the webpage as results, and open the containing folders by simply clicking on a link.

 

 

3.  I'm in the windows world mainly.  BUT, I do have a linux machine available to me as well, that I added to our domain the other day, but I'm having issues with that... that's a whole different forum.

 

 

4.  Thank you all in advance for any help or guidance or pointers you can give me.  I'm in it to learn. ;)

 

-Pedro

Link to comment
Share on other sites

I am not sure how to do this, but to clear this up for the other members.

 

You want a PHP script to open up a text file and find terms that match what the user inputted?

Then you want a link to the directory it was in?

Link to comment
Share on other sites

Umm, maybe? Lamez.   

 

This is correct, exactly what the first part is!

You want a PHP script to open up a text file and find terms that match what the user inputted?

 

 

Well, I want a link to the directory that the file is in that matched the search term.  Remember, the text file I have is a file that has one (1) UNC path per line... and there are over a million lines.  Each UNC path, is the full path to the file that people are searching for.  But the link to be displayed should only open the directory that that matched file is in, not the file itself.

Then you want a link to the directory it was in?

 

 

 

Link to comment
Share on other sites

try

<?php
function find_dir($needle, $file = 'test.txt'){
$needle = '/'.trim($needle);
$lines = file($file);
foreach ($lines as $line){
	$line = trim($line);
	if (strpos($line, $needle)){
		$out = str_replace($needle, '',$line);
		$out = str_replace('//', '',$out);
		$out = explode('/', $out);
		return '<a href="file://'.implode('\\',$out).'">'.$out[count($out)-1].'</a>';
	}
}
return false;
}
echo find_dir('mypic.tif');
?>

Link to comment
Share on other sites

How large is this file? Can it be read to memory all at once?

 

$lines = file( 'yourtext.txt' );

$term = 'something';

foreach ( $lines as $line ) {
    if ( stripos($line, $term) !== FALSE )
        echo '<a href="file://'.$line.'">'.$line.'</a>';
}

Link to comment
Share on other sites

All, thanks a lot everyone for their help. ;D ;D ;D  I'm still working on this, probably will be fudging with it all weekend.

 

To answer adam84, that would open the file, but I need to be able to output search results in a linked format, kind of what sasa has in their code. 

 

Sasa, let me play around with what you've started me there.  I really appreciate it.

 

discomatt- The file is almost 200MB.  So, opening it over a network all at once may not work out so well.

Link to comment
Share on other sites

All, thanks a lot everyone for their help. ;D ;D ;D  I'm still working on this, probably will be fudging with it all weekend.

 

To answer adam84, that would open the file, but I need to be able to output search results in a linked format, kind of what sasa has in their code. 

 

Sasa, let me play around with what you've started me there.  I really appreciate it.

 

discomatt- The file is almost 200MB.  So, opening it over a network all at once may not work out so well.

 

this is all server side, so unless your webserver is opening the file from a network share, no network traffic will be involved in reading the file.  since the file is so large I would go with fgets() instead of file() or fread().  fgets() only reads one line at a time, saving memory.  I don't see mysql being an option here as it appears he is parsing server logs of some sort...and there is nothing wrong with playing with flat-files.  People were doing this with C and Fortran long before most of you were born.

 

<?php
function find_dir($needle, $file = 'test.txt'){
$matches = array();
        $needle = '/'.trim($needle);

        $fh = @fopen($file, "r");

        if (!$fh) return 0;

        while (!feof($fh)) {
        
            $line = fgets($handle);

  	     if (strpos($line, $needle)){
		$out = str_replace($needle, '',$line);
		$out = str_replace('//', '',$out);
		$out = explode('/', $out);
		$matches[] = '<a href="file://'.implode('\\',$out).'">'.$out[count($out)-1].'</a>';
	}

       
    }
    fclose($fh);


return $matches;
}

?>

 

I just cannibalized sasa's code, replacing the array line by line operations with file line by line operations, so don't get mad at me if it doesn't work.  It should be pretty close though.

 

 

with that being said..... if this is linux it may be faster just to backtick grep:

 

 

<?
$file = '/some/file';
$search = "some-term";

$matched_lines = preg_split("/\n/",`cat $file |grep $search`);
?>

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.