Page 1 of 1

Help debug this file?

Posted: Sun Sep 03, 2006 11:00 pm
by cursed
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I hope this is clear enough.

Code: Select all

<?php
function deleteLine($searchQuery, $filename) {
   $file = file($filename);
   $write = '';
   for($i=0; $i < count($file); $i++)
      if(strpos($file[$i], $searchQuery) === false)
         $write .= $file[$i];
   $handle = fopen('link.txt', 'w');
   fwrite($handle, $write);
   fclose($handle);
}

if (in_array(test, file('link.txt'))) { 
   echo 'works.';
   } else {   
    if(strpos(file_get_contents('http://www.test.com/'), 'test') !== false) {
      $filename = 'link.txt';
      $somecontent = "\n Test.com <BR> Description: A test website<HR><HR> \n";

      // Let's make sure the file exists and is writable first.
      if (is_writable($filename)) {
         // In our example we're opening $filename in append mode.
         // The file pointer is at the bottom of the file hence 
         // that's where $somecontent will go when we fwrite() it.
         if (!($handle = fopen($filename, 'a')))
            die("Cannot open file $filename.");

         // Write $somecontent to our opened file.
         if (fwrite($handle, $somecontent) === false)
            die("Failed to write to $filename.");

         echo "SUCCESS.  \"$somecontent\" was written to file $filename.";
         fclose($handle);

      }
   } else deleteLine('test','link.txt');
}
?>
The

Code: Select all

if (in_array(test, file('link.txt'))) { 
   echo 'works.';
part isnt working.

Any advice would be appreciated.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sun Sep 03, 2006 11:13 pm
by toasty2

Code: Select all

if (in_array(test, file('link.txt'))) {
   echo 'works.';
Are you sure you didn't mean $test or "test"?

Also, don't use code tags for php code on the forums. Change the tag from code to php.

Posted: Sun Sep 03, 2006 11:16 pm
by feyd
A description of what you're trying to do here would probably help.

Posted: Sun Sep 03, 2006 11:17 pm
by toasty2
feyd, how do you type the php tag without it parsing? (When you correct people's posts)

Posted: Sun Sep 03, 2006 11:18 pm
by feyd
[php[u][/u]] or similar.

Posted: Mon Sep 04, 2006 11:58 am
by cursed
This script does:
It first searches another persons site, then appends to a text file.
If the search query isn't there, then it deletes a line in a text file.
So far, everything works except

Code: Select all

if (in_array(test, file('link.txt'))) { 
   echo 'works.';
That suppose to see if test is already there, in the file link.txt
Only that part isn't working so far.

Posted: Mon Sep 04, 2006 12:07 pm
by feyd
file() returns an array of the lines (including the carriage returns) in a given file. In the posted snippet "test" (provided you are searching for the string) would need to be literally the sole thing on that line (no whitespace around it) to be found.

Is this your intention?

Posted: Mon Sep 04, 2006 12:09 pm
by cursed
Not really, is there a way to make it in any word like testing and such?

Posted: Mon Sep 04, 2006 12:16 pm
by feyd
That would probably be a loop + strpos()/stripos() type of look see.

Posted: Mon Sep 04, 2006 8:13 pm
by cursed
can u give me an example using strpos?

i know i used it in the code i posted, its just that im not familiar with it.