Doom750 Posted July 11, 2008 Share Posted July 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/114318-use-php-to-scan-txt-file-line-by-line/ Share on other sites More sharing options...
Lamez Posted July 11, 2008 Share Posted July 11, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/114318-use-php-to-scan-txt-file-line-by-line/#findComment-587852 Share on other sites More sharing options...
Doom750 Posted July 11, 2008 Author Share Posted July 11, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/114318-use-php-to-scan-txt-file-line-by-line/#findComment-587874 Share on other sites More sharing options...
adam84 Posted July 11, 2008 Share Posted July 11, 2008 Something like this? $myFile = "file.txt"; $file = fopen($myFile, 'r'); while( !feof( $file ) ){ echo fgets($file) . "<BR>"; } fclose($file); Quote Link to comment https://forums.phpfreaks.com/topic/114318-use-php-to-scan-txt-file-line-by-line/#findComment-587880 Share on other sites More sharing options...
sasa Posted July 11, 2008 Share Posted July 11, 2008 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'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/114318-use-php-to-scan-txt-file-line-by-line/#findComment-587891 Share on other sites More sharing options...
discomatt Posted July 11, 2008 Share Posted July 11, 2008 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/114318-use-php-to-scan-txt-file-line-by-line/#findComment-587901 Share on other sites More sharing options...
Doom750 Posted July 11, 2008 Author Share Posted July 11, 2008 All, thanks a lot everyone for their help. ;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. Quote Link to comment https://forums.phpfreaks.com/topic/114318-use-php-to-scan-txt-file-line-by-line/#findComment-587917 Share on other sites More sharing options...
discomatt Posted July 11, 2008 Share Posted July 11, 2008 sasa's code will attempt to read the file to memory as well. You're doing this a difficult, slow way though... if you want it searchable you should use a database language like MySQL. Quote Link to comment https://forums.phpfreaks.com/topic/114318-use-php-to-scan-txt-file-line-by-line/#findComment-587922 Share on other sites More sharing options...
mbeals Posted July 11, 2008 Share Posted July 11, 2008 All, thanks a lot everyone for their help. ;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`); ?> Quote Link to comment https://forums.phpfreaks.com/topic/114318-use-php-to-scan-txt-file-line-by-line/#findComment-587946 Share on other sites More sharing options...
mbeals Posted July 11, 2008 Share Posted July 11, 2008 sorry, went to edit the last post to fix the short tags and must of hit quote instead of modify note: you could also use explode instead of preg_split in the last example. Would be faster Quote Link to comment https://forums.phpfreaks.com/topic/114318-use-php-to-scan-txt-file-line-by-line/#findComment-587957 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.