DeathStar Posted June 19, 2007 Share Posted June 19, 2007 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 More sharing options...
Corona4456 Posted June 19, 2007 Share Posted June 19, 2007 Please state your problem in further detail. Not sure what the exact nature of your problem is. Link to comment https://forums.phpfreaks.com/topic/56253-solved-search-for-string-the-delete/#findComment-277881 Share on other sites More sharing options...
DeathStar Posted June 19, 2007 Author Share Posted June 19, 2007 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? Link to comment https://forums.phpfreaks.com/topic/56253-solved-search-for-string-the-delete/#findComment-277886 Share on other sites More sharing options...
Corona4456 Posted June 19, 2007 Share Posted June 19, 2007 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); ?> Link to comment https://forums.phpfreaks.com/topic/56253-solved-search-for-string-the-delete/#findComment-277898 Share on other sites More sharing options...
DeathStar Posted June 19, 2007 Author Share Posted June 19, 2007 That just does nothing. Just returns the same. Link to comment https://forums.phpfreaks.com/topic/56253-solved-search-for-string-the-delete/#findComment-277909 Share on other sites More sharing options...
Corona4456 Posted June 19, 2007 Share Posted June 19, 2007 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); } ?> Link to comment https://forums.phpfreaks.com/topic/56253-solved-search-for-string-the-delete/#findComment-277910 Share on other sites More sharing options...
DeathStar Posted June 19, 2007 Author Share Posted June 19, 2007 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. Link to comment https://forums.phpfreaks.com/topic/56253-solved-search-for-string-the-delete/#findComment-277935 Share on other sites More sharing options...
Corona4456 Posted June 19, 2007 Share Posted June 19, 2007 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. Link to comment https://forums.phpfreaks.com/topic/56253-solved-search-for-string-the-delete/#findComment-277938 Share on other sites More sharing options...
DeathStar Posted June 19, 2007 Author Share Posted June 19, 2007 Thnakyou, that seemed to work Link to comment https://forums.phpfreaks.com/topic/56253-solved-search-for-string-the-delete/#findComment-277944 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.