PHP Scan Directory For Key Words or String

Experts,
My boss had me create a resume database for our company using PHP and MS SQL.  I have finished it using a file on the server for the actual uploaded docs and the target in the database along with all other information entered on a form according to the transaction number.  I now need the ability to scan all the docs in the directory for keywords and dump all the hits into an array.  Can anyone help me or point me to an example script that I can modify?
kwilliams123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jayarjoCommented:
What's your problem then? If your files are not very big and are actually text files, then you can do something like this:

$text = file_get_contents(PATH_TO_THE_FILE);

preg_match_all('/KEYWORD/i', $text, $matches); // $matches[0] will hold all your hits for KEYWORD

Although I don't think that this piece of code is actually useful. I guess you need to re-phrase your problem, 'cause it's not exactly obvious, what kind of hits you are trying to accumulate in the array.
$text = file_get_contents(PATH_TO_THE_FILE);

preg_match_all('/KEYWORD/i', $text, $matches); // $matches[0] will hold all your hits for KEYWORD

Open in new window

0
PhatzerCommented:
I hope this helps you out a little...
<?php

// Set keywords to match
$keywords = array(
    'keyword1',
    'keyword2'
);

// Set array to record hits
$documenthits = array();

// Open directory
$dir = opendir('/path/to/documents');

// If directory is open
if ($dir) {
    
    // Loop over files in directory
    while (false !== ($document = readdir($dir))) {
        
        // If file is not a directory
        if (!is_dir($document)) {
            
            // Iterate over keywords
            foreach ($keywords as $kw) {
                
                // If filename matches keyword
                if (strstr(basename($document), $kw)) {
                    
                    // Add file path to array and break loop
                    $documenthits[] = $document;
                    break;
                    
                }
                
            }
            
        }
        
    }
    
}

?>

Open in new window

0
PhatzerCommented:
Sorry, I misunderstood, I thought you wanted to iterate over the file names and not the file contents. The script I posted would need some minor changes in order to accomplish this, however (if you'd like me to).
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

kwilliams123Author Commented:
Thanks Phatzer, do you mind?
0
PhatzerCommented:
I'm not about the computer right now, but if somebody doesn't beat me to it, I'll have it posted in the morning. Sorry for the hassle, but I hope I've at least got the ball rolling for you.
0
PhatzerCommented:
Sorry for the delay. If the files are stored in plain text, or otherwise readable without the assistance of Word or alike, then the following script should at least get you started. If the documents are in Word format, I'm sure there are libraries out there to assist.
<?php

// Set keywords to match
$keywords = array(
    'keyword1',
    'keyword2'
);

// Set array to record hits
$documenthits = array();

// Open directory
$dir = opendir('/path/to/documents');

// If directory is open
if ($dir) {
    
    // Loop over files in directory
    while (false !== ($document = readdir($dir))) {
        
        // If file is not a directory
        if (!is_dir($document)) {
            
            // Iterate over keywords
            foreach ($keywords as $kw) {
                
                // Read file contents
                $documentcontents = file_get_contents($document);
                
                // If document contents match keyword
                if (strstr($documentcontents, $kw)) {
                    
                    // Add file path to array and break loop
                    $documenthits[] = $document;
                    break;
                    
                }
                
            }
            
        }
        
    }
    
}

?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.