开发者

searching a line in a txt file

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜