How to delete first 11 lines in a file using PHP?
up vote
2
down vote
favorite
I have a CSV file in which I want the first 11 lines to be removed. The file looks something like:
"MacroTrends Data Download"
"GOOGL - Historical Price and Volume Data"
"Historical prices are adjusted for both splits and dividends"
"Disclaimer and Terms of Use: Historical stock data is provided 'as is' and solely for informational purposes, not for trading purposes or advice."
"MacroTrends LLC expressly disclaims the accuracy, adequacy, or completeness of any data and shall not be liable for any errors, omissions or other defects in, "
"delays or interruptions in such data, or for any actions taken in reliance thereon. Neither MacroTrends LLC nor any of our information providers will be liable"
"for any damages relating to your use of the data provided."
date,open,high,low,close,volume
2004-08-19,50.1598,52.1911,48.1286,50.3228,44659000
2004-08-20,50.6614,54.7089,50.4056,54.3227,22834300
2004-08-23,55.5515,56.9157,54.6938,54.8694,18256100
2004-08-24,55.7922,55.9728,51.9454,52.5974,15247300
2004-08-25,52.5422,54.1672,52.1008,53.1641,9188600
I want only the stocks data and not anything else. So I wish to remove the first 11 lines. Also, there will be several text files for different tickers. So str_replace
doesn't seem to be a viable option. The function I've been using to get CSV file and putting the required contents to a text file is
function getCSVFile($url, $outputFile)
{
$content = file_get_contents($url);
$content = str_replace("date,open,high,low,close,volume", "", $content);
$content = trim($content);
file_put_contents($outputFile, $content);
}
I want a general solution which can remove the first 11 lines from the CSV file and put the remaining contents to a text file. How do I do this?
php csv file-get-contents php-7 file-handling
add a comment |
up vote
2
down vote
favorite
I have a CSV file in which I want the first 11 lines to be removed. The file looks something like:
"MacroTrends Data Download"
"GOOGL - Historical Price and Volume Data"
"Historical prices are adjusted for both splits and dividends"
"Disclaimer and Terms of Use: Historical stock data is provided 'as is' and solely for informational purposes, not for trading purposes or advice."
"MacroTrends LLC expressly disclaims the accuracy, adequacy, or completeness of any data and shall not be liable for any errors, omissions or other defects in, "
"delays or interruptions in such data, or for any actions taken in reliance thereon. Neither MacroTrends LLC nor any of our information providers will be liable"
"for any damages relating to your use of the data provided."
date,open,high,low,close,volume
2004-08-19,50.1598,52.1911,48.1286,50.3228,44659000
2004-08-20,50.6614,54.7089,50.4056,54.3227,22834300
2004-08-23,55.5515,56.9157,54.6938,54.8694,18256100
2004-08-24,55.7922,55.9728,51.9454,52.5974,15247300
2004-08-25,52.5422,54.1672,52.1008,53.1641,9188600
I want only the stocks data and not anything else. So I wish to remove the first 11 lines. Also, there will be several text files for different tickers. So str_replace
doesn't seem to be a viable option. The function I've been using to get CSV file and putting the required contents to a text file is
function getCSVFile($url, $outputFile)
{
$content = file_get_contents($url);
$content = str_replace("date,open,high,low,close,volume", "", $content);
$content = trim($content);
file_put_contents($outputFile, $content);
}
I want a general solution which can remove the first 11 lines from the CSV file and put the remaining contents to a text file. How do I do this?
php csv file-get-contents php-7 file-handling
1
file_put_contents(implode('', array_slice(file($url), 12)));
– splash58
Nov 8 at 13:20
@IslamElshobokshy The string in the first 11 lines will change depending on the text files. So I don't think it is a duplicate of that question.
– Gautam Vashisht
Nov 8 at 13:20
Possible duplicate of skip first line of fgetcsv method in php
– user3783243
Nov 8 at 13:36
^ .. but just extend one of those methods to 11.
– user3783243
Nov 8 at 13:36
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a CSV file in which I want the first 11 lines to be removed. The file looks something like:
"MacroTrends Data Download"
"GOOGL - Historical Price and Volume Data"
"Historical prices are adjusted for both splits and dividends"
"Disclaimer and Terms of Use: Historical stock data is provided 'as is' and solely for informational purposes, not for trading purposes or advice."
"MacroTrends LLC expressly disclaims the accuracy, adequacy, or completeness of any data and shall not be liable for any errors, omissions or other defects in, "
"delays or interruptions in such data, or for any actions taken in reliance thereon. Neither MacroTrends LLC nor any of our information providers will be liable"
"for any damages relating to your use of the data provided."
date,open,high,low,close,volume
2004-08-19,50.1598,52.1911,48.1286,50.3228,44659000
2004-08-20,50.6614,54.7089,50.4056,54.3227,22834300
2004-08-23,55.5515,56.9157,54.6938,54.8694,18256100
2004-08-24,55.7922,55.9728,51.9454,52.5974,15247300
2004-08-25,52.5422,54.1672,52.1008,53.1641,9188600
I want only the stocks data and not anything else. So I wish to remove the first 11 lines. Also, there will be several text files for different tickers. So str_replace
doesn't seem to be a viable option. The function I've been using to get CSV file and putting the required contents to a text file is
function getCSVFile($url, $outputFile)
{
$content = file_get_contents($url);
$content = str_replace("date,open,high,low,close,volume", "", $content);
$content = trim($content);
file_put_contents($outputFile, $content);
}
I want a general solution which can remove the first 11 lines from the CSV file and put the remaining contents to a text file. How do I do this?
php csv file-get-contents php-7 file-handling
I have a CSV file in which I want the first 11 lines to be removed. The file looks something like:
"MacroTrends Data Download"
"GOOGL - Historical Price and Volume Data"
"Historical prices are adjusted for both splits and dividends"
"Disclaimer and Terms of Use: Historical stock data is provided 'as is' and solely for informational purposes, not for trading purposes or advice."
"MacroTrends LLC expressly disclaims the accuracy, adequacy, or completeness of any data and shall not be liable for any errors, omissions or other defects in, "
"delays or interruptions in such data, or for any actions taken in reliance thereon. Neither MacroTrends LLC nor any of our information providers will be liable"
"for any damages relating to your use of the data provided."
date,open,high,low,close,volume
2004-08-19,50.1598,52.1911,48.1286,50.3228,44659000
2004-08-20,50.6614,54.7089,50.4056,54.3227,22834300
2004-08-23,55.5515,56.9157,54.6938,54.8694,18256100
2004-08-24,55.7922,55.9728,51.9454,52.5974,15247300
2004-08-25,52.5422,54.1672,52.1008,53.1641,9188600
I want only the stocks data and not anything else. So I wish to remove the first 11 lines. Also, there will be several text files for different tickers. So str_replace
doesn't seem to be a viable option. The function I've been using to get CSV file and putting the required contents to a text file is
function getCSVFile($url, $outputFile)
{
$content = file_get_contents($url);
$content = str_replace("date,open,high,low,close,volume", "", $content);
$content = trim($content);
file_put_contents($outputFile, $content);
}
I want a general solution which can remove the first 11 lines from the CSV file and put the remaining contents to a text file. How do I do this?
php csv file-get-contents php-7 file-handling
php csv file-get-contents php-7 file-handling
asked Nov 8 at 13:14
Gautam Vashisht
1911213
1911213
1
file_put_contents(implode('', array_slice(file($url), 12)));
– splash58
Nov 8 at 13:20
@IslamElshobokshy The string in the first 11 lines will change depending on the text files. So I don't think it is a duplicate of that question.
– Gautam Vashisht
Nov 8 at 13:20
Possible duplicate of skip first line of fgetcsv method in php
– user3783243
Nov 8 at 13:36
^ .. but just extend one of those methods to 11.
– user3783243
Nov 8 at 13:36
add a comment |
1
file_put_contents(implode('', array_slice(file($url), 12)));
– splash58
Nov 8 at 13:20
@IslamElshobokshy The string in the first 11 lines will change depending on the text files. So I don't think it is a duplicate of that question.
– Gautam Vashisht
Nov 8 at 13:20
Possible duplicate of skip first line of fgetcsv method in php
– user3783243
Nov 8 at 13:36
^ .. but just extend one of those methods to 11.
– user3783243
Nov 8 at 13:36
1
1
file_put_contents(implode('', array_slice(file($url), 12)));
– splash58
Nov 8 at 13:20
file_put_contents(implode('', array_slice(file($url), 12)));
– splash58
Nov 8 at 13:20
@IslamElshobokshy The string in the first 11 lines will change depending on the text files. So I don't think it is a duplicate of that question.
– Gautam Vashisht
Nov 8 at 13:20
@IslamElshobokshy The string in the first 11 lines will change depending on the text files. So I don't think it is a duplicate of that question.
– Gautam Vashisht
Nov 8 at 13:20
Possible duplicate of skip first line of fgetcsv method in php
– user3783243
Nov 8 at 13:36
Possible duplicate of skip first line of fgetcsv method in php
– user3783243
Nov 8 at 13:36
^ .. but just extend one of those methods to 11.
– user3783243
Nov 8 at 13:36
^ .. but just extend one of those methods to 11.
– user3783243
Nov 8 at 13:36
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
Every example here won't work for large/huge files. People don't care about the memory nowadays. You, as a great programmer, want your code to be efficient with low memory footprint.
Instead parse file line by line:
function saveStrippedCsvFile($inputFile, $outputFile, $lineCountToRemove)
{
$inputHandle = fopen($inputFile, 'r');
$outputHandle = fopen($outputFile, 'w');
// make sure you handle errors as well
// files may be unreadable, unwritable etc…
$counter = 0;
while (!feof($inputHandle)) {
if ($counter < $lineCountToRemove) {
++$counter;
continue;
}
fwrite($outputHandle, fgets($inputHandle) . PHP_EOL);
}
fclose($inputHandle);
fclose($outputHandle);
}
add a comment |
up vote
1
down vote
I have a CSV file in which I want the first 11 lines to be removed.
I always prefer to use explode
to do that.
$string = file_get_contents($file);
$lines = explode('n', $string);
for($i = 0; $i < 11; $i++) { //First key = 0 - 0,1,2,3,4,5,6,7,8,9,10 = 11 lines
unset($lines[$i]);
}
This will remove it and with implode
you can create a new 'file' out of it
$new = implode('n',$lines);
$new
will contain the new file
Did'nt test it, but I'm pretty sure that this will work
Be carefull! I will quote @emix his comment.
This will fail spectacularly if the file content exceeds available PHP memory.
Be sure that the file isn't to 'huge'
1
That will remove 12 lines. You wanna write$i < 11
instead of$i <= 11
.
– Bartosz Zasada
Nov 8 at 13:22
Thanks, I see. Was a typo :)
– Koen Hollander
Nov 8 at 13:23
1
This will fail spectacularly if the file content exceeds available PHP memory.
– emix
Nov 8 at 13:34
add a comment |
up vote
0
down vote
Use file()
to read it as array and simply trim first 11 lines:
$content = file($url);
$newContent = array_slice($content, 12);
file_put_contents($outputFile, implode(PHP_EOL, $newContent));
But answer these questions:
- Why there is additional content in this CSV?
- How will you know how much lines to cut off? What if it's more than 11 lines to cut?
I downloaded the stocks data from Macrotrends. All the file contains some extra information about Macrotrends in the first 11 lines. After that, it is just the stocks data which I want.
– Gautam Vashisht
Nov 8 at 13:34
This will fail spectacularly if the file content exceeds available PHP memory.
– emix
Nov 8 at 13:45
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Every example here won't work for large/huge files. People don't care about the memory nowadays. You, as a great programmer, want your code to be efficient with low memory footprint.
Instead parse file line by line:
function saveStrippedCsvFile($inputFile, $outputFile, $lineCountToRemove)
{
$inputHandle = fopen($inputFile, 'r');
$outputHandle = fopen($outputFile, 'w');
// make sure you handle errors as well
// files may be unreadable, unwritable etc…
$counter = 0;
while (!feof($inputHandle)) {
if ($counter < $lineCountToRemove) {
++$counter;
continue;
}
fwrite($outputHandle, fgets($inputHandle) . PHP_EOL);
}
fclose($inputHandle);
fclose($outputHandle);
}
add a comment |
up vote
2
down vote
accepted
Every example here won't work for large/huge files. People don't care about the memory nowadays. You, as a great programmer, want your code to be efficient with low memory footprint.
Instead parse file line by line:
function saveStrippedCsvFile($inputFile, $outputFile, $lineCountToRemove)
{
$inputHandle = fopen($inputFile, 'r');
$outputHandle = fopen($outputFile, 'w');
// make sure you handle errors as well
// files may be unreadable, unwritable etc…
$counter = 0;
while (!feof($inputHandle)) {
if ($counter < $lineCountToRemove) {
++$counter;
continue;
}
fwrite($outputHandle, fgets($inputHandle) . PHP_EOL);
}
fclose($inputHandle);
fclose($outputHandle);
}
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Every example here won't work for large/huge files. People don't care about the memory nowadays. You, as a great programmer, want your code to be efficient with low memory footprint.
Instead parse file line by line:
function saveStrippedCsvFile($inputFile, $outputFile, $lineCountToRemove)
{
$inputHandle = fopen($inputFile, 'r');
$outputHandle = fopen($outputFile, 'w');
// make sure you handle errors as well
// files may be unreadable, unwritable etc…
$counter = 0;
while (!feof($inputHandle)) {
if ($counter < $lineCountToRemove) {
++$counter;
continue;
}
fwrite($outputHandle, fgets($inputHandle) . PHP_EOL);
}
fclose($inputHandle);
fclose($outputHandle);
}
Every example here won't work for large/huge files. People don't care about the memory nowadays. You, as a great programmer, want your code to be efficient with low memory footprint.
Instead parse file line by line:
function saveStrippedCsvFile($inputFile, $outputFile, $lineCountToRemove)
{
$inputHandle = fopen($inputFile, 'r');
$outputHandle = fopen($outputFile, 'w');
// make sure you handle errors as well
// files may be unreadable, unwritable etc…
$counter = 0;
while (!feof($inputHandle)) {
if ($counter < $lineCountToRemove) {
++$counter;
continue;
}
fwrite($outputHandle, fgets($inputHandle) . PHP_EOL);
}
fclose($inputHandle);
fclose($outputHandle);
}
answered Nov 8 at 13:40
emix
5,86142747
5,86142747
add a comment |
add a comment |
up vote
1
down vote
I have a CSV file in which I want the first 11 lines to be removed.
I always prefer to use explode
to do that.
$string = file_get_contents($file);
$lines = explode('n', $string);
for($i = 0; $i < 11; $i++) { //First key = 0 - 0,1,2,3,4,5,6,7,8,9,10 = 11 lines
unset($lines[$i]);
}
This will remove it and with implode
you can create a new 'file' out of it
$new = implode('n',$lines);
$new
will contain the new file
Did'nt test it, but I'm pretty sure that this will work
Be carefull! I will quote @emix his comment.
This will fail spectacularly if the file content exceeds available PHP memory.
Be sure that the file isn't to 'huge'
1
That will remove 12 lines. You wanna write$i < 11
instead of$i <= 11
.
– Bartosz Zasada
Nov 8 at 13:22
Thanks, I see. Was a typo :)
– Koen Hollander
Nov 8 at 13:23
1
This will fail spectacularly if the file content exceeds available PHP memory.
– emix
Nov 8 at 13:34
add a comment |
up vote
1
down vote
I have a CSV file in which I want the first 11 lines to be removed.
I always prefer to use explode
to do that.
$string = file_get_contents($file);
$lines = explode('n', $string);
for($i = 0; $i < 11; $i++) { //First key = 0 - 0,1,2,3,4,5,6,7,8,9,10 = 11 lines
unset($lines[$i]);
}
This will remove it and with implode
you can create a new 'file' out of it
$new = implode('n',$lines);
$new
will contain the new file
Did'nt test it, but I'm pretty sure that this will work
Be carefull! I will quote @emix his comment.
This will fail spectacularly if the file content exceeds available PHP memory.
Be sure that the file isn't to 'huge'
1
That will remove 12 lines. You wanna write$i < 11
instead of$i <= 11
.
– Bartosz Zasada
Nov 8 at 13:22
Thanks, I see. Was a typo :)
– Koen Hollander
Nov 8 at 13:23
1
This will fail spectacularly if the file content exceeds available PHP memory.
– emix
Nov 8 at 13:34
add a comment |
up vote
1
down vote
up vote
1
down vote
I have a CSV file in which I want the first 11 lines to be removed.
I always prefer to use explode
to do that.
$string = file_get_contents($file);
$lines = explode('n', $string);
for($i = 0; $i < 11; $i++) { //First key = 0 - 0,1,2,3,4,5,6,7,8,9,10 = 11 lines
unset($lines[$i]);
}
This will remove it and with implode
you can create a new 'file' out of it
$new = implode('n',$lines);
$new
will contain the new file
Did'nt test it, but I'm pretty sure that this will work
Be carefull! I will quote @emix his comment.
This will fail spectacularly if the file content exceeds available PHP memory.
Be sure that the file isn't to 'huge'
I have a CSV file in which I want the first 11 lines to be removed.
I always prefer to use explode
to do that.
$string = file_get_contents($file);
$lines = explode('n', $string);
for($i = 0; $i < 11; $i++) { //First key = 0 - 0,1,2,3,4,5,6,7,8,9,10 = 11 lines
unset($lines[$i]);
}
This will remove it and with implode
you can create a new 'file' out of it
$new = implode('n',$lines);
$new
will contain the new file
Did'nt test it, but I'm pretty sure that this will work
Be carefull! I will quote @emix his comment.
This will fail spectacularly if the file content exceeds available PHP memory.
Be sure that the file isn't to 'huge'
edited Nov 8 at 13:36
answered Nov 8 at 13:20
Koen Hollander
84921428
84921428
1
That will remove 12 lines. You wanna write$i < 11
instead of$i <= 11
.
– Bartosz Zasada
Nov 8 at 13:22
Thanks, I see. Was a typo :)
– Koen Hollander
Nov 8 at 13:23
1
This will fail spectacularly if the file content exceeds available PHP memory.
– emix
Nov 8 at 13:34
add a comment |
1
That will remove 12 lines. You wanna write$i < 11
instead of$i <= 11
.
– Bartosz Zasada
Nov 8 at 13:22
Thanks, I see. Was a typo :)
– Koen Hollander
Nov 8 at 13:23
1
This will fail spectacularly if the file content exceeds available PHP memory.
– emix
Nov 8 at 13:34
1
1
That will remove 12 lines. You wanna write
$i < 11
instead of $i <= 11
.– Bartosz Zasada
Nov 8 at 13:22
That will remove 12 lines. You wanna write
$i < 11
instead of $i <= 11
.– Bartosz Zasada
Nov 8 at 13:22
Thanks, I see. Was a typo :)
– Koen Hollander
Nov 8 at 13:23
Thanks, I see. Was a typo :)
– Koen Hollander
Nov 8 at 13:23
1
1
This will fail spectacularly if the file content exceeds available PHP memory.
– emix
Nov 8 at 13:34
This will fail spectacularly if the file content exceeds available PHP memory.
– emix
Nov 8 at 13:34
add a comment |
up vote
0
down vote
Use file()
to read it as array and simply trim first 11 lines:
$content = file($url);
$newContent = array_slice($content, 12);
file_put_contents($outputFile, implode(PHP_EOL, $newContent));
But answer these questions:
- Why there is additional content in this CSV?
- How will you know how much lines to cut off? What if it's more than 11 lines to cut?
I downloaded the stocks data from Macrotrends. All the file contains some extra information about Macrotrends in the first 11 lines. After that, it is just the stocks data which I want.
– Gautam Vashisht
Nov 8 at 13:34
This will fail spectacularly if the file content exceeds available PHP memory.
– emix
Nov 8 at 13:45
add a comment |
up vote
0
down vote
Use file()
to read it as array and simply trim first 11 lines:
$content = file($url);
$newContent = array_slice($content, 12);
file_put_contents($outputFile, implode(PHP_EOL, $newContent));
But answer these questions:
- Why there is additional content in this CSV?
- How will you know how much lines to cut off? What if it's more than 11 lines to cut?
I downloaded the stocks data from Macrotrends. All the file contains some extra information about Macrotrends in the first 11 lines. After that, it is just the stocks data which I want.
– Gautam Vashisht
Nov 8 at 13:34
This will fail spectacularly if the file content exceeds available PHP memory.
– emix
Nov 8 at 13:45
add a comment |
up vote
0
down vote
up vote
0
down vote
Use file()
to read it as array and simply trim first 11 lines:
$content = file($url);
$newContent = array_slice($content, 12);
file_put_contents($outputFile, implode(PHP_EOL, $newContent));
But answer these questions:
- Why there is additional content in this CSV?
- How will you know how much lines to cut off? What if it's more than 11 lines to cut?
Use file()
to read it as array and simply trim first 11 lines:
$content = file($url);
$newContent = array_slice($content, 12);
file_put_contents($outputFile, implode(PHP_EOL, $newContent));
But answer these questions:
- Why there is additional content in this CSV?
- How will you know how much lines to cut off? What if it's more than 11 lines to cut?
answered Nov 8 at 13:23
Justinas
26.8k33356
26.8k33356
I downloaded the stocks data from Macrotrends. All the file contains some extra information about Macrotrends in the first 11 lines. After that, it is just the stocks data which I want.
– Gautam Vashisht
Nov 8 at 13:34
This will fail spectacularly if the file content exceeds available PHP memory.
– emix
Nov 8 at 13:45
add a comment |
I downloaded the stocks data from Macrotrends. All the file contains some extra information about Macrotrends in the first 11 lines. After that, it is just the stocks data which I want.
– Gautam Vashisht
Nov 8 at 13:34
This will fail spectacularly if the file content exceeds available PHP memory.
– emix
Nov 8 at 13:45
I downloaded the stocks data from Macrotrends. All the file contains some extra information about Macrotrends in the first 11 lines. After that, it is just the stocks data which I want.
– Gautam Vashisht
Nov 8 at 13:34
I downloaded the stocks data from Macrotrends. All the file contains some extra information about Macrotrends in the first 11 lines. After that, it is just the stocks data which I want.
– Gautam Vashisht
Nov 8 at 13:34
This will fail spectacularly if the file content exceeds available PHP memory.
– emix
Nov 8 at 13:45
This will fail spectacularly if the file content exceeds available PHP memory.
– emix
Nov 8 at 13:45
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208508%2fhow-to-delete-first-11-lines-in-a-file-using-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
file_put_contents(implode('', array_slice(file($url), 12)));
– splash58
Nov 8 at 13:20
@IslamElshobokshy The string in the first 11 lines will change depending on the text files. So I don't think it is a duplicate of that question.
– Gautam Vashisht
Nov 8 at 13:20
Possible duplicate of skip first line of fgetcsv method in php
– user3783243
Nov 8 at 13:36
^ .. but just extend one of those methods to 11.
– user3783243
Nov 8 at 13:36