ninevolt1 Posted July 22, 2009 Share Posted July 22, 2009 I am a complete PHP hack. So I need some help deleting the first five lines in a text file. It is a RSS feed file called news_rss.xml. The first five lines in the file are: 1 2<!--ARTICLE--> 3<?xml version="1.0" encoding="UTF-8"?> 4<rss version="2.0"> 5<channel> I seem to have figured out the code to open and save the file. Where I have indicated CODE CODE CODE is where I would like to put the code that will delete those above lines (I tried cutting and pasting other code from these forums but they didn't work for me): $content = file_get_contents("news_rss.xml"); CODE CODE CODE file_put_contents("news_rss.xml", "$newcontent"); Thanks for any help. Quote Link to comment Share on other sites More sharing options...
ldougherty Posted July 22, 2009 Share Posted July 22, 2009 Since file_get_contents stores the file content as a string you can only set it to start at a certain character. Now if you know exactly how many characters it is you want to remove from the beginning and its the same each time then you could specify this. The better option would likely be to store the file into an array using the file() function. http://us3.php.net/manual/en/function.file.php Once you have the array created you can simply remove the first 5 items in the array. http://www.w3schools.com/PHP/func_array_splice.asp Hope that helps.. Quote Link to comment Share on other sites More sharing options...
ninevolt1 Posted July 22, 2009 Author Share Posted July 22, 2009 Thanks for the quick reply. After reading those pages, I think building from scratch is over my head :'(. If we completely take out my 3 lines of code (I only intended to use them because I thought they would do the trick of opening and saving the file) and go the route suggested, would you or anyone else be able to quickly post the code here that I should use. Thanks again for your help! Quote Link to comment Share on other sites More sharing options...
TomNomNom Posted July 22, 2009 Share Posted July 22, 2009 Thanks for the quick reply. After reading those pages, I think building from scratch is over my head :'(. If we completely take out my 3 lines of code (I only intended to use them because I thought they would do the trick of opening and saving the file) and go the route suggested, would you or anyone else be able to quickly post the code here that I should use. Thanks again for your help! Here you are, sir :-) <?php $content = file_get_contents('news_rss.xml'); $content = explode("\n", $content); array_splice($content, 0, 5); $newcontent = implode("\n", $content); file_put_contents('news_rss.xml', $newcontent); Quote Link to comment Share on other sites More sharing options...
ldougherty Posted July 22, 2009 Share Posted July 22, 2009 Is there a reason to use file_get_contents then explode to make the array rather than just using file() from the get go? Quote Link to comment Share on other sites More sharing options...
TomNomNom Posted July 22, 2009 Share Posted July 22, 2009 Is there a reason to use file_get_contents then explode to make the array rather than just using file() from the get go? Only to maintain the OP's original idea :-) <?php $content = file('data.xml'); array_splice($content, 0, 5); file_put_contents('newdata.xml', $content); ^^^ That's what I would do personally - but I try my best to work around the way people have figured things out. Otherwise their efforts were all for naught :-) Quote Link to comment Share on other sites More sharing options...
ninevolt1 Posted July 22, 2009 Author Share Posted July 22, 2009 Thank you so much for code and time and effort! Works great. Quote Link to comment Share on other sites More sharing options...
ninevolt1 Posted July 22, 2009 Author Share Posted July 22, 2009 Hey, I'm back! What would I change in this script to make it only delete lines 3,4,5? I'm using the 2nd script by the way since you all seem to agree that it is better to do it that way. Quote Link to comment Share on other sites More sharing options...
9three Posted July 22, 2009 Share Posted July 22, 2009 change the parameters in array_splice to be 3, 5 If you read the documentation on array_splice it explains it clearly. http://www.php.net/manual/en/function.array-splice.php Quote Link to comment Share on other sites More sharing options...
ninevolt1 Posted July 22, 2009 Author Share Posted July 22, 2009 change the parameters in array_splice to be 3, 5 If you read the documentation on array_splice it explains it clearly. http://www.php.net/manual/en/function.array-splice.php Thanks! I was actually doing that while you graciously responded to help me out. BTW, it needs to be 3,3 (start on the third line and take out 3 lines--I'm sure your above post was a typo). Quote Link to comment 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.