• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 317
  • Last Modified:

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?
0
kwilliams123
Asked:
kwilliams123
  • 4
1 Solution
 
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
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

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now