Jump to content

Using stripos


unknown101

Recommended Posts

Hi guys, I have the following snippet of my code..

 


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

      //Initialise the $results variable here

         if ( stripos($results, $line))
         echo 'The source has '. $line .' in it<BR>';

 

So basically im reading in data from a text file and checking to see if the value exists within the $results variable.  If I echo the $results var it shows all the data im searching and if I echo the $line it shows me the line its read in from the file so i know this is also working - at the moment im using a word which i know exists within $results so it should echo out that its been identified, but it doesnt.

 

If I manually set the $line variable rather than reading this in from the file it works fine so i know the actual function works.  Something must be happening to the value when passed from a file, but I've run out of things to try:| Can anyone suggest why this may be happening?

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/102961-using-stripos/
Share on other sites

In this example $myFile doesn't appear to be  valid filename.

 

Are you getting any error message when you try to open the file

 

Have you used var_dump($line) after using fgets() to make sure that you actually got some thing.

 

You are not checking the return codes on your functions. You should check the results and see if you are getting any errors

 

Here is more on checking your function error codes: http://www.phpcodinghelp.com/article.php?article=debug#basic_function_results

Link to comment
https://forums.phpfreaks.com/topic/102961-using-stripos/#findComment-527447
Share on other sites

Thanks for the quick reply, Ill have a look into checking return codes in future. I just tried one more thing... deleting the file and re-creating it and now seems to work:|

 

The next problem im having is looping through the values which are being read in from the text file.

 

For example if I have the following:

 


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

      

while ( $line = fgets($fh, 1000))
  {

        //Initialise the $results variable here

         if ( stripos($results, $line))
         echo 'The source has '. $line .' in it<BR>';
  }

 

For testing I have entered 3 different words all on seperate lines within the textfile.  When I run the above it only seems to print out the last word which was in the textfile.  i.e. If I had the 3 words "John", "James" and "Ben" in that order it would just print:

 

The source has Ben in it .. without printing the the first 2 - any ideas why that could be happening ?

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/102961-using-stripos/#findComment-527454
Share on other sites

Hey guys ive made some slight changes..im using the class called "Snoopy" to help extract some data from a URL.

 

This is my code:

 

do
    {

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


echo "Search: $line<BR>";

  if (stripos($results, $line))

   {
   echo ' The term has been found;
   }

   else
  {
  echo "The Keyterm HASNT been found<BR>";
  }

}
while ( $line = fgets($fh, 1000));

 

The keyterms im searched for are read in from a txt file as mentioned above and i can echo these out to the browser so i know thats working.  I can also echo the $result variable which shows all the data will be searched to find a match.  But when i run this it never finds any of the key terms, but I know they exist (as I have checked a test URL for certain words and added these to my text file im reading in from).

 

Can anyone suggest why this may be happening?

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/102961-using-stripos/#findComment-527497
Share on other sites

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.