tags:

views:

63

answers:

0

This is part of a routine that searches for a word.

The folders to search are like this:$entirelist. / a/0 thru a/z all the way to z/0 z/z.

The files in those directories are one single column of words.

"aardark" would be in $entirelist/a/a

a-ardvark would be in $entirelist/a/sc (sc = special char in second position. There will never be on if first position.)

I am trying to check for dupes here before I append a new name to one of those files.

(this is in a for loop so the $goodnodupe[$g] has been established. (not a dupe in that particular array_unique))

$sc = array("-", "_", ".", "@", "+", "~");
for ($g=0;$g<$gd; $g++){
if($goodnodupe[$g] != ''){
$word = $goodnodupe[$g];  # word to search
        $top = substr($word, 0, 1);
        $sub = substr($word, 1, 1);
        if (in_array($sub, $sc)) {
        $sub='sc';
        }
        $allfiles = glob($entirelist."/".$top."/".$sub."/*.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){
        if (preg_match("/\b".$word."\b/i", $existing)) {
        echo "A match was found.";
        } else {
        echo "A match was not found.";
        } 
        }

This is pretty verbose I am quite the noob. The above is working but, it does not stop after it has found a match and echos "~not found "for every line in every file in the $entirelist/a/a folder. (re: aardvark).

I think I am close but, no cigar.

I also have this snippet which searches files which looks promising but, I dont need the file functions. Wondering if I can implement sections of it in the above question. I am still trying!

$words = array('a', 'b', 'c'); # words to insert, assumed to be unique
$fp = fopen('words.txt', 'r+');
while (!feof($fp))
{
$line = trim(fgets($fp));
$key = array_search($line, $words);
if ($key !== false)
{
unset($words[$key]);
if (!$words) break;
}
}
foreach ($words as $word)
{
fputs($fp, "$word\n");
}
fclose($fp);