PHP - Stop searching when match is found
Incoming is a single word from a text box or another function
function searchformatch($keyword){
if(!$keyword){hey-ent开发者_开发百科er-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.
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.
精彩评论