I have a txt file in the server and it contains lines sth like that
one
two
three
four
fiv开发者_StackOverflow社区e
i want to make a function that checks if a word exists in these lines.
any help is appreciated.
thank you
Here is how you may proceed with it:
$contents = file_get_contents('yourfile.txt');
$search_keyword = 'four';
// check if word is there
if (strpos($contents, $search_keyword) !== FALSE){
echo "$search_keyword was found !!";
}
else{
echo "$search_keyword was NOT found !!";
}
Does this really need to be done in php? You've just described the UNIX grep
utility.
You could do it like:
$data = file($path_to_file,FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if (in_array($data,'word')) {
// do something
}
That is a simple hack but it should work.
精彩评论