$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);
}
}