Jump to content

[SOLVED] Read a text file and search for a string


pragan

Recommended Posts

Hey Guys,

 

I am trying to get the following code done but I am not able to proceed after some point. Please help me with this.

 

I am trying to read a text file(contents is in this form: a,b,cde,fgh,) and then look for a string(obtained from a mysql query) in this text file.

 

I am able to query the database and get the result, read the text file, explode it to get the words line by line but I am not able to match for the query result in the text file output. Please help me out

Ex: If the query result has the value "cde" then I need to look in the text file and say word found else say not found.

 

Waiting for any reply.

 

regards

pragan.

 

 

Link to comment
Share on other sites

thanks for the reply. I guess I have to use array_search as the text contents are stored in array format after  I explode them.

Again one more question :

If I use

array_search9$queryresult, $textcontents),

its not doing the search but if I say the word directly

array_search("testing", $textcontents)

its able to say found/not found...Also I am getting the output like 7 times (i.e. found notfound not found..notfound..) I do not understand this.

 

Please help me out.

Link to comment
Share on other sites

There are a couple things to consider. First, if you are just doing the loop, you need to break out of the loop when the string is found. That way, you don't continue to process and report on the rest of the file when you have already found the result. Also, if you use a string search, you can avoid having to worry about breaking the string into pieces. Try something like this:

 

<?php
function fileSearch($filename, $string)
{
  $handle = fopen($filename, 'r');
  while (!feof($handle))
  {
    $row = fgets($handle, 4096);
    $pattern = '|\b' . $string . '\b|'; // Check for word boundaries
    if (preg_match($pattern, $row))
    {
      // Break out of the loop
      fclose($handle);
      return TRUE;
    }
  }

  return FALSE; // nothing was found
}

if (fileSearch('filename.txt', 'my-string'))
{
  // String appears in document
}
?>

 

Of course, if your text files are not super long, you can always just get the whole thing as a string and then do your boundary search on it:

<?php
$string = 'abc';
$text = file_get_contents('filename.txt');
if (strstr($text, $string))
{
  // At least one occurrence found.
}
?>

 

Good luck!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.