Log in

View Full Version : Deleting a line of a TXT file.



cursed
08-28-2006, 10:44 PM
I want this script to instead of saying you're a guest, into deleting a line in a TXT file.

Is this possible?

Below is the script I'm using.

if(stripos(file_get_contents('http://www.othersite.com/page.html'), '<script')!== false)
echo('YES');
else
echo('NO')



also is it possible to "search" for a tag or something in the text code and delete it?

blm126
08-28-2006, 11:17 PM
What does the text file look like? This is very possible but we need a little more information.

cursed
08-29-2006, 12:34 AM
something like

<a href="http://www.google.com">
Google Search Engine </a>
<BR>
Description: A really famous search engine. If you dont know what this is... well.. eh..... um.... crawl under a rock.

blm126
08-29-2006, 01:30 AM
Is that all on one line? If not what line do you want deleted?

cursed
08-29-2006, 01:47 AM
yeah, if its possible to be deleted...

blm126
08-29-2006, 03:59 PM
I'm still not quite clear on what the file looks like. Could you provide an example with more than one? Also please make use of the code tags.

cursed
08-29-2006, 06:01 PM
i just want to delete, say 4-5 lines of HTML code.
such as:

<B>code</B>here....<BR>random html code.

blm126
08-29-2006, 06:18 PM
What distinguishes the lines you want to keep from the lines you want removed.

cursed
08-29-2006, 06:55 PM
erm.. can u show me some code of how to do it, and ill figure a way to do that. :)

blm126
08-29-2006, 11:51 PM
Ok,Here is a quick 'n dirty example.
list.txt looks like this


http://www.dynamicdrive.com
http://www.digg.com
http://www.yahoo.com
http://www.google.com
http://www.ebay.com

and index.php looks like this


<html>
<head>
</head>
<body>
<ul>
<?php
if(!empty($_GET['line'])){
$file = file('list.txt');
ob_start();
for($i=0;$i < count($file);$i++){
if(rtrim($file[$i]) != $_GET['line']){
echo $file[$i];
}
}
$write = ob_get_contents();
ob_end_clean();
$handle = fopen('list.txt','w');
fwrite($handle,$write);
fclose($handle);
}
$file = file('list.txt');
for($i=0;$i<count($file);$i++){
echo '<li><a href="index.php?line='.urlencode(rtrim($file[$i])).'">Delete '.rtrim($file[$i]).'</a></li>'."\n";
}
?>
</ul>
</body>
</html>

They both need to be in the same directory. Go ahead and run to see what it does

cursed
08-29-2006, 11:55 PM
thanks, but can u help me 'automatically' do it and somehow use preg_replace or str_replace?

blm126
08-30-2006, 12:00 AM
'automatically' based on what?

cursed
08-30-2006, 12:04 AM
acutrally..

when a website is visited, then it will delete text.

blm126
08-30-2006, 12:12 AM
Ok, I see the connection now. That is code I gave you before right? Not sure how to test this so...it is untested


<html>
<head>
</head>
<body>
<?php
$affiliate = 'somesite.com'; //Don't add anything that isn't ALWAYS in the URL
if(strpos(strtolower($_SERVER['HTTP_REFERER']),$affiliate) !== false){
$add = $affiliate;
}
else{
$del = $affiliate;
}
if(!empty($del)){
$file = file('list.txt');
ob_start();
for($i=0;$i < count($file);$i++){
if(rtrim($file[$i]) != $del){
echo $file[$i];
}
}
$write = ob_get_contents();
ob_end_clean();
$handle = fopen('list.txt','w');
fwrite($handle,$write);
fclose($handle);
}
else if(!empty($add)){
$handle = fopen('list.txt','a');
fwrite($handle,$add."\n");
fclose($handle);
}
?>
</body>
</html>

blm126
08-30-2006, 12:13 AM
Ok, yeah list.txt needs to look like this


dynamicdrive.com
digg.com
yahoo.com
google.com
ebay.com

cursed
08-30-2006, 12:15 AM
what will this delete? Only the url, right?
i want it to delete some text after the url. hope im not bothering you with all my questions

blm126
08-30-2006, 08:54 PM
I'm still not quite clear on what the file looks like. Could you provide an example with more than one

erm.. can u show me some code of how to do it, and ill figure a way to do that. :)
So, I repeat. what does the file look like exactly?

cursed
08-30-2006, 10:07 PM
<a href="http://www.urlhere.com">Test</a> <BR> Description: A test website about blah.

blm126
08-31-2006, 01:40 AM
<?php
function deleteline($del,$filename){
$file = file($filename);
ob_start();
for($i=0;$i < count($file);$i++){
if(strpos($file[$i],$del) === FALSE){
echo $file[$i];
}
}
$write = ob_get_contents();
ob_end_clean();
$handle = fopen('list.txt','w');
fwrite($handle,$write);
fclose($handle);
}
//Pass in the search term, then the filename
deleteline('google','list.txt');
?>

Here I've written a function to do what you want. I'll leave it up to you to determine how to use it.

cursed
08-31-2006, 02:49 AM
thanks, oh grand PHP coder.

this will delete every line that has the word google in it, right?

blm126
08-31-2006, 10:23 PM
correct. Be careful this is case-sensitive. google is not the same as GooGLE

cursed
08-31-2006, 11:01 PM
thanks blm, :) i know it wasnt easy.

quick question, what does /n do?

blm126
08-31-2006, 11:25 PM
/n is the newline character. It is mentioned briefly in the manual here (http://us2.php.net/trim)