Stephayne
Stephayne

Reputation: 11

php How to search files in a directory for an exact line match?

$allfiles = glob($searchdir."*.txt");
$elist = array();
foreach($allfiles as $file){
    $lines = array_merge($elist, file($file, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES));
}
foreach ($lines as $existing){
    // this echos a number // echo "<br />Existing".$existing."<br />";
    if (preg_match("/\b".$searchforthis."\b/i", $existing)) {
        echo "A match was found.";
        continue;
    } else {
        echo "A match was not found.";
        $nodupe ="y";
        continue;
    }
}

In the above I am attempting to check for a match in a directory of files and return a true or false for the next step.

It is obviously not working. I echoed the line in attempt to troubleshoot but, I get a number not the word on the line.

The file(s) being searched single column of words with only 100 lines each. There may be up to 5 of them in the directory.

I echoed the paths and other vars and all is correct. In just never finds a match.

I know I should learn mysql but, I need this to work.

I am also unsure about the continue or break. This routine resides in a for loop inside an if.

I want this to stop looking when a match was found.

Thanks for any guidance.

I added the file write part here in case I messed that up causing the problem. It writes a number to the file and does not append even when I bypass the append switch below and null the statement that does not..

/****Write the entry if no match******/
if ($nodupe != "y"){
    if($append == "n"){
        $name .= $searchforthis."\n";
        file_put_contents($filepath.$writefile.".txt", $name, LOCK_EX);
    }
    else{
        file_put_contents($filepath.$writefile.".txt", $name, FILE_APPEND | LOCK_EX);
    } 
}     

Upvotes: 1

Views: 2636

Answers (2)

Alex Howansky
Alex Howansky

Reputation: 53563

The /m modifier will search across lines, so you don't need to scan each line individually:

$search = 'whatever';
foreach (glob($dir . '/*.txt') as $file) {
    if (preg_match('/^' . $search . '$/m', file_get_contents($file))) {
        echo "$file contains $search\n";
        break;
    } else {
        echo "$file does not contain $search\n";
    }
}

Alternatively, if your word lists don't change very much, you'll be better off just making them into PHP arrays and include()'ing them straight into your script:

$list = array(
    'word1',
    'word2',
    'word3',
    // ...
);

And then you can just use in_array() to scan for the word.

Upvotes: 1

ts.
ts.

Reputation: 10709

Try this:

<?php

# variables

$sDir = __DIR__; # php 5.3, for php <5.3 use dirname(__FILE__);
$sFilePattern = '*.php';
$sSearch = 'printf';

# config

$sRegExp = '/\b'.$sSearch.'\b/i';

# code

foreach (glob($sDir . DIRECTORY_SEPARATOR . $sFilePattern) as $sFile){

    foreach (file($sFile) as $nLineNumber => $sLine){

        if (preg_match($sRegExp, $sLine) == 1){

            printf('<br/>Word "%s" found in %s, line %d', $sSearch, $sFile, $nLineNumber);

        } // if

    } // foreach

} // foreach

It's literally the same thing that yours. Should show 2 occurences of 'printf'.

Upvotes: 1

Related Questions