tags:

views:

43

answers:

2
$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);
    } 
}     
+1  A: 

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.

Alex Howansky
Cool, will this match the exact word? Not dog in doggie?
Stephayne
like: if(in_array($list, $search)) {dothis();} ?? thank you...
Stephayne
That works great! Matches the word and nothing but the exact word. That was a great help. Now if I can just figure out what is wrong with the file write section!
Stephayne
No, switch your parameters around: if (in_array($search, $list)) { dothis(); }
Alex Howansky
Yes this will match the whole word. Note the regexp is /^pattern$/ -- where ^ means start of line and $ means end of line. So, this will match "start of line, pattern, end of line" and nothing else. If you want to match dog for doggie then just remove the ^ and the $ and it will match the word anywhere in the line.
Alex Howansky
+1  A: 

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'.

ts
Thank you, this looks very good to me. Above my level but, I think I am understanding. I will hit the php manual and attempt to understand this fully.
Stephayne