unknown101 Posted April 28, 2008 Share Posted April 28, 2008 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 More sharing options...
soycharliente Posted April 28, 2008 Share Posted April 28, 2008 Maybe we can help with the logic of your code? Mind sharing it? Also, can you give an example of a website and some terms? By looking at your code, we could either diagnose the problem or help you find a better alternative. Link to comment https://forums.phpfreaks.com/topic/103310-matching-strings-in-txt/#findComment-529110 Share on other sites More sharing options...
unknown101 Posted April 28, 2008 Author Share Posted April 28, 2008 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 Link to comment https://forums.phpfreaks.com/topic/103310-matching-strings-in-txt/#findComment-529167 Share on other sites More sharing options...
unknown101 Posted April 28, 2008 Author Share Posted April 28, 2008 Any ideas at all guys ? Cheers Link to comment https://forums.phpfreaks.com/topic/103310-matching-strings-in-txt/#findComment-529188 Share on other sites More sharing options...
kenrbnsn Posted April 28, 2008 Share Posted April 28, 2008 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 Link to comment https://forums.phpfreaks.com/topic/103310-matching-strings-in-txt/#findComment-529198 Share on other sites More sharing options...
unknown101 Posted April 28, 2008 Author Share Posted April 28, 2008 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 Link to comment https://forums.phpfreaks.com/topic/103310-matching-strings-in-txt/#findComment-529204 Share on other sites More sharing options...
kenrbnsn Posted April 28, 2008 Share Posted April 28, 2008 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 Link to comment https://forums.phpfreaks.com/topic/103310-matching-strings-in-txt/#findComment-529207 Share on other sites More sharing options...
unknown101 Posted April 28, 2008 Author Share Posted April 28, 2008 Thank you very much, thats finally working:) Your help is much appreciated. Link to comment https://forums.phpfreaks.com/topic/103310-matching-strings-in-txt/#findComment-529212 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.