tags:

views:

30

answers:

1

Incoming is a single word from a text box or another function

function searchformatch($keyword){

              if(!$keyword){hey-enter-a-keyword-function();}
                foreach (glob("directory/*.txt") as $filename) {
                 $file = $filename;
                 $contents = file($file);
                 $string = implode($contents);
                   foreach ($string as $look){
                     if (preg_match("/\b".$keyword."\b/i", $look) {
                /*echo $string;*/
                      }
                   }
                 }
             }

This is my attempt to try to look for an exact line match in some files in a directory. (each line is one word.)

But, I want it to stop searching when a match is found.

I also want to do this in another function if $keyword is an array of keywords. (eg: in from a text area.) But, that is probably another question.

+1  A: 

Use break to stop the foreach loop if a match was found:

foreach ($string as $look) {
    if (preg_match("/\b".$keyword."\b/i", $look)) {
        /*echo $string;*/
        break;
    }
}

With break 2 you can stop both loops.

Gumbo
Wow, excellent. Thank you. As far as the second part of my question, what if $keyword an array? Or should I post another question?
Staphayne