Utku Dalmaz
Utku Dalmaz

Reputation: 10162

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
five

i want to make a function that checks if a word exists in these lines.

any help is appreciated.

thank you

Upvotes: 1

Views: 183

Answers (3)

Sarfraz
Sarfraz

Reputation: 382666

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 !!";
}

Upvotes: 2

futureal
futureal

Reputation: 3045

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.

Upvotes: 0

Amber
Amber

Reputation: 526573

Does this really need to be done in php? You've just described the UNIX grep utility.

Upvotes: 2

Related Questions