speaker219 Posted July 9, 2007 Share Posted July 9, 2007 Hello, how would I code a php script that would open a text file, look for a string i specified, and if it finds that string, it will delete the entire line in the text file that contained that text. I'm new to php, so sorry if this is really obvious.. :-\ Any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/59117-solved-delete-entire-line-in-text-file-if-anything-in-the-line-matches-search-string/ Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 <?php if ($file=file("file.txt")) { foreach ($file as $txt) { if (stripos("search",$txt)!==false) { echo 'Found: '.$txt.'<br />'; } } } else { echo 'Cannot read the file'; } ?> "string" is what you're wanting to look for - replace this with a variable containing the search text. Quote Link to comment https://forums.phpfreaks.com/topic/59117-solved-delete-entire-line-in-text-file-if-anything-in-the-line-matches-search-string/#findComment-293549 Share on other sites More sharing options...
aim25 Posted July 9, 2007 Share Posted July 9, 2007 First use file_get_contents() to get the stuff in the txt file, then use str_replace() to chage a part of it. Quote Link to comment https://forums.phpfreaks.com/topic/59117-solved-delete-entire-line-in-text-file-if-anything-in-the-line-matches-search-string/#findComment-293553 Share on other sites More sharing options...
metrostars Posted July 9, 2007 Share Posted July 9, 2007 <?php $content = file_get_contents("filename.txt"); $newcontent = str_replace("The string you want to find", "", "$content"); file_put_contents("filename.txt", "$newcontent"); ?> should do the trick. Quote Link to comment https://forums.phpfreaks.com/topic/59117-solved-delete-entire-line-in-text-file-if-anything-in-the-line-matches-search-string/#findComment-293554 Share on other sites More sharing options...
trq Posted July 9, 2007 Share Posted July 9, 2007 Another easy method if your using Linux is to use the Linux util sed..... <?php exec("sed -i -e '/texthere/d' filehere"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/59117-solved-delete-entire-line-in-text-file-if-anything-in-the-line-matches-search-string/#findComment-293556 Share on other sites More sharing options...
speaker219 Posted July 9, 2007 Author Share Posted July 9, 2007 I'm not sure you guys understand. I want it to delete the entire line after finding a specific string on that line. for example if the text file was: Hello. this is line 1. hello this is line 2 hello waka line 3 hello this is line 4 The key word is "waka" and I want the php script to delete the entire line of text that is on the same line as the word "waka" so it would look like this: Hello. this is line 1. hello this is line 2 hello this is line 4 thanks. Quote Link to comment https://forums.phpfreaks.com/topic/59117-solved-delete-entire-line-in-text-file-if-anything-in-the-line-matches-search-string/#findComment-293557 Share on other sites More sharing options...
aim25 Posted July 9, 2007 Share Posted July 9, 2007 use this then. <?php $key = "waka"; //load file into $fc array $fc=file("some.txt"); //open same file and use "w" to clear file $f=fopen("some.txt","w"); //loop through array using foreach foreach($fc as $line) { if (!strstr($line,$key)) //look for $key in each line fputs($f,$line); //place $line back in file } fclose($f); ?> Quote Link to comment https://forums.phpfreaks.com/topic/59117-solved-delete-entire-line-in-text-file-if-anything-in-the-line-matches-search-string/#findComment-293563 Share on other sites More sharing options...
speaker219 Posted July 9, 2007 Author Share Posted July 9, 2007 Thanks alot, worked nicely. Great forum here too it's set up here: http://speaker219.ath.cx:8080/test.php and the text file is : http://speaker219.ath.cx:8080/test.txt Quote Link to comment https://forums.phpfreaks.com/topic/59117-solved-delete-entire-line-in-text-file-if-anything-in-the-line-matches-search-string/#findComment-293568 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.