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!
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.
<?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');
}
?>
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]
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
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.