Jump to content

Matching strings in txt


unknown101

Recommended Posts

Hi Guys,

 

Im trying to create a script which gets the content of a website and searches it for key terms which are stored in a text file.

 

Now I can get the content and assign it to a variable no problem, but ive been trying for hours on end to loop through the txt file key terms and use them to compare with the $source website. 

 

Ive been using the "Stripos" function i.e.    if ( stripos($source, $keyterm) ) { ..do this... } and it just wont work.

 

Are there any other ways to search for a value within a load of text in PHP?

 

Any suggestions appreciated..

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/103310-matching-strings-in-txt/
Share on other sites

I am using a class called "Snoopy", which I downloaded from the net .. I think if you just google snoopy php its one of the first hits (Just useful for fetching web content etc)

 

My code is as follows:

 


<?php

include "Snoopy.class.php";
$snoopy = new Snoopy;

$myFile = "KeyTerms.txt";
$fh = fopen($myFile, "r");
$line = fgets($fh);

$snoopy->fetchtext("http://www.any-domain.com");
            $results = $snoopy->results;

            do
           {
           $names = array("$line");
   
   		foreach ($names as $value)
   			 {
		   echo "Search: $value<BR>";

		   if ( stripos($results, $value) )

		      {
   			     echo The term '. $value .' has been found;
	                   }
                             else
                               {
                             echo No match found<BR>';
                                }
                            }
}
while ( $line = fgets($fh, 1000));	

?>

 

If for example I run this code and have 4 values on seperate lines in a text file which I know are on the site which has been fetched, it will only print to say its found the last value listed in the txt file??  I dont know why its not looping through all values? PLzzzzzzzzzz help,

 

Thanks in advance

 

 

Please do not bump your question so quickly. Also, you have asked this same question at least twice before and you should have continued the same thread.

 

As I posted in one of your other threads -- please show use the contents of the file you're reading.

 

Ken

ok Sorry.

 

The keyterms.txt file just literally contains a few words i've been testing with.  Say I scan Google.co.uk, I just add a few words which I know are on that homepage so I can test it, also one which i know isnt.  The file just looks like:

 

Keywords.txt..

 

Google

Images

Maps

ThisIsntOnThere

 

Dont know what else to try:|

 

Thanks

 

 

Putting the line of read from the file into an array is useless. Try this:

<?php

include "Snoopy.class.php";
$snoopy = new Snoopy;

$terms = array_map('trim',file("KeyTerms.txt"));
$snoopy->fetchtext("http://www.any-domain.com");
$results = $snoopy->results;

foreach ($terms as $value) {
        echo "Search: $value<BR>";
        if ( stripos($results, $value) )
   	      echo The term '. $value .' has been found;
        else
              echo No match found<BR>';
}
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.