Jump to content

How to remove first 5 lines from a text file


ninevolt1

Recommended Posts

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.

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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 :-)

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.