Jump to content

[SOLVED] Search for string, the delete.


DeathStar

Recommended Posts

Hello there,

 

I am currently busy making an shoutbox.

But here is my problem.

I have the whole system working great, put in bccodes etc.

 

But I am now adding an future that you will be able to delete the shout/post.

Here is how it must work:

Search for a string within the text file, then remove the line that the string is in and and put back the other contents.

The text files contents is formatted like this:

1:Name:%3Cb%3EWelcome to your shoutbox!%3Cb%3E
2:Name:%3Cb%3EWelcome to your shoutbox2!%3Cb%3E

 

My best attempt is this:

 

<?php
$var['id'] = addslashes($_GET['id']);
/* Get Post id. */
$fc=file("chat.txt");
//open same file and use "w" to clear file
$f=fopen("chat.txt","w");
//loop through array using foreach
foreach($fc as $line)
{
list($id, $name, $message) = explode(':',$line);
      if ($id = $var['id']){
       $string = $id . ':' . $name . ':' . $message;
	$do = str_replace($string,'',$line);
      fputs($f,$line);
      header('Location: chat.php');
} 
else{
	echo 'None dound.';
}
}
fclose($f);

 

If there is a way i can edit aswell please also let me know.

 

Thank you.

DeathStar

Link to comment
https://forums.phpfreaks.com/topic/56253-solved-search-for-string-the-delete/
Share on other sites

Well, currently it is just emptying the whole file.

 

Here is an example:

say I want to remove the string where the id is 1:

0:Name:Text
1:Name:Text
2:Name:Text

 

The output must be:

0:Name:Text
2:Name:Text

 

Any further explanation?

Ah ok, you are doing a little too much work, here's a simplified version:

<?php
$var['id'] = addslashes($_GET['id']);
/* Get Post id. */
$fc=file("chat.txt");
//loop through array using foreach
$file_contents = '';
foreach($fc as $line)
{
list($id, $name, $message) = explode(':',$line);
      
      if ($id != $var['id']){
      	$file_contents .= $line;
      header('Location: chat.php');
} 
else{
	echo 'None dound.';
}
}
//open same file and use "w" to clear file
$f=fopen("chat.txt","w");
fputs($f, $file_contents);
fclose($f);
?>

Did you pass in an 'id' to your url?  Such as 'scriptname.php?id=2'...

 

Here's a better version to check of $_GET['id'] is set.

<?php
if(isset($_GET['id'])){
$var['id'] = addslashes($_GET['id']);
echo $var['id'];
/* Get Post id. */
$fc=file(dirname(__FILE__) . "/chat.txt");
//loop through array using foreach
$file_contents = '';
foreach($fc as $line)
{
	list($id, $name, $message) = explode(':',$line);
      
  if ($id != $var['id']){
    $file_contents .= $line;
	}	else{
		echo 'None dound.';
	}
}
//open same file and use "w" to clear file
$f=fopen(dirname(__FILE__) . "/chat.txt","w");
fputs($f, $file_contents);
fclose($f);
}
?>

Thank you, but I don't quite think you understand.

 

I do not want it to echo, I want it to delete the line that the id is in, like I posted.

That current script is the part I can manage to make, but I want to remove the line.

Err... crap... I accidentally left that in there. Sorry:

 

<?php
if(isset($_GET['id'])){
$var['id'] = addslashes($_GET['id']);
/* Get Post id. */
$fc=file(dirname(__FILE__) . "/chat.txt");
//loop through array using foreach
$file_contents = '';
foreach($fc as $line)
{
	list($id, $name, $message) = explode(':',$line);
      
  if ($id != $var['id']){
    $file_contents .= $line;
            header('Location: chat.php');
	}	else{
		echo 'None found.';
	}
}
//open same file and use "w" to clear file
$f=fopen(dirname(__FILE__) . "/chat.txt","w");
fputs($f, $file_contents);
fclose($f);
}
?>

 

Also, I've tested this code and I know it works.

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.