What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » Searching text files

Searching text files
Author: Message:
-dt-
Scripting Contest Winner
*****

Avatar
;o

Posts: 1819
Reputation: 74
35 / Male / Flag
Joined: Mar 2004
RE: Searching text files
quote:
Originally posted by methos
quote:
Originally posted by blessedguy
quote:
Originally posted by methos
It'd be quite easy in PHP...
How? =O
Not nearly as easy as WDZ's method above, but just for completion and curiosity...
PHP code:
$needle = 'needle'; //value to search in the text files
$dir = './dir/'; //dir to folder containing text files
 
$files = scandir($dir);
$arr_count = count($files);
$i = 0;
$cur_file = current($files);
 
while ($i<$arr_count){
    $ext = substr($cur_file, strrpos($cur_file,'.') + 1);
    if($ext == 'txt'){
        $line_array = file($dir.$cur_file);
        $line_count = count(file($dir.$cur_file));
        $n = 1;
        $cur_line = current($line_array);
        while ($n <= $line_count){
            if(strpos($cur_line,$needle) !== FALSE){
                $line_quote = str_replace($needle,"<b>".$needle."</b>",$cur_line);
                echo"Found value in $dir"."$cur_file <br /> $n - $line_quote";
                exit;
            }
            $n++;
            $cur_line = next($line_array);
        }
    }
    $i++;
    $cur_file = next($files);
}
 

Creates an array of the contents of ./dir then progressively checks if each value is a text file, if so, each line of this text file is read into an array, which is then progressively searched for the search value $needle. When $needle is found, it stops searching and the path to the file is echo'd, along with the line number and quote of the line with $needle in bold. Can be easily modified to create a file results.txt with this info in, and to search through all files etc.

:happy:
its even easier than that...

PHP code:
<?php
 
$dir = "D:/";
$toFind = "xxx";
 
foreach(new DirectoryIterator($dir) as $file){
    if(!$file->isDir()){
        $ext = trim(strrchr($file, '.'), ".");
        if($ext == "txt"){  
            $fp = fopen($file->getPathname(), 'r');
            while($fp && !feof($fp)){
                $line = stream_get_line($fp, 2048, "\n");
                if(strpos($line, $toFind) !== false){
                    echo $file . "\n";
                }
            }
            fclose($fp);
        }
    }
}
 
 
 
 
?>

[Image: dt2.0v2.png]      Happy Birthday, WDZ
05-04-2009 12:15 AM
Profile PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
Searching text files - by Jimbo on 05-02-2009 at 03:45 PM
RE: Searching text files - by blessedguy on 05-02-2009 at 03:50 PM
RE: Searching text files - by Jimbo on 05-02-2009 at 04:18 PM
RE: Searching text files - by Baggins on 05-02-2009 at 04:55 PM
RE: Searching text files - by stoshrocket on 05-02-2009 at 07:15 PM
RE: Searching text files - by blessedguy on 05-02-2009 at 07:20 PM
RE: Searching text files - by Jimbo on 05-02-2009 at 07:30 PM
RE: Searching text files - by L. Coyote on 05-02-2009 at 08:05 PM
RE: Searching text files - by Jarrod on 05-02-2009 at 10:12 PM
RE: Searching text files - by WDZ on 05-03-2009 at 01:01 AM
RE: Searching text files - by Jimbo on 05-03-2009 at 05:45 PM
RE: Searching text files - by stoshrocket on 05-03-2009 at 07:27 PM
RE: Searching text files - by -dt- on 05-04-2009 at 12:15 AM
RE: Searching text files - by stoshrocket on 05-04-2009 at 12:35 AM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On