Help debug this file?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cursed
Forum Newbie
Posts: 16
Joined: Sat Aug 26, 2006 10:18 pm

Help debug this file?

Post 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]
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post 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.
Last edited by toasty2 on Sun Sep 03, 2006 11:16 pm, edited 2 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

A description of what you're trying to do here would probably help.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

feyd, how do you type the php tag without it parsing? (When you correct people's posts)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[php[u][/u]] or similar.
cursed
Forum Newbie
Posts: 16
Joined: Sat Aug 26, 2006 10:18 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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?
cursed
Forum Newbie
Posts: 16
Joined: Sat Aug 26, 2006 10:18 pm

Post by cursed »

Not really, is there a way to make it in any word like testing and such?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That would probably be a loop + strpos()/stripos() type of look see.
cursed
Forum Newbie
Posts: 16
Joined: Sat Aug 26, 2006 10:18 pm

Post 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.
Post Reply