Asked  7 Months ago    Answers:  5   Viewed   30 times

I'm trying to read a specific line from a text file using php. Here's the text file:


How would I get the content of the second line using php? This returns the first line:

$myFile = "4-24-11.txt";
$fh = fopen($myFile, 'r');
$theData = fgets($fh);
echo $theData;

..but I need the second.

Any help would be greatly appreciated


$myFile = "4-24-11.txt";
$lines = file($myFile);//file in to an array
echo $lines[1]; //line 2

file — Reads entire file into an array

Wednesday, March 31, 2021
answered 7 Months ago

It happens because PHP CS Fixer modifies the file, as @LazyOne said, but you have empty value of "Output paths to refresh" so IDE cannot know about these changes.

Set the value of "Output paths to refresh" to $FileName$ - same as in arguments - to make PhpStorm aware about the changes (it depends on the "Working directory" value which has been set in Other Options - if it is set to $FileDir$ then you don't need to mention it in the paths to refresh).

Wednesday, March 31, 2021
answered 7 Months ago

Using splfileobject

  • no need to read all lines 1 by 1

  • can "jump" to desired line

In the case you know the line number :

//lets say you need line 4
$myLine = 4 ; 
$file = new SplFileObject('bigFile.txt');
//this is zero based so need to subtract 1
//now print the line
echo $file->current();

check out :

Saturday, May 29, 2021
answered 5 Months ago

I've started project called Big File Tools. It is proven to work on Linux, Mac and Windows (even 32-bit variants). It provides byte-precise results even for huge files (>4GB). Internally it uses brick/math - arbitrary-precision arithmetic library.

Install it using composer.

composer install jkuchar/BigFileTools

and use it:

$file = BigFileToolsBigFileTools::createDefault()->getFile(__FILE__);
echo $file->getSize() . " bytesn";

Result is BigInteger so you can compute with results

$sizeInBytes = $file->getSize();
$sizeInMegabytes = $sizeInBytes->toBigDecimal()->dividedBy(1024*1024, 2, BrickMathRoundingMode::HALF_DOWN);    
echo "Size is $sizeInMegabytes megabytesn";

Big File Tools internally uses drivers to reliably determine exact file size on all platforms. Here is list of available drivers (updated 2016-02-05)

| Driver           | Time (s) ?          | Runtime requirements | Platform 
| ---------------  | ------------------- | --------------       | ---------
| CurlDriver       | 0.00045299530029297 | CURL extension       | -
| NativeSeekDriver | 0.00052094459533691 | -                    | -
| ComDriver        | 0.0031449794769287  | COM+.NET extension   | Windows only
| ExecDriver       | 0.042937040328979   | exec() enabled       | Windows, Linux, OS X
| NativeRead       | 2.7670161724091     | -                    | -

You can use BigFileTools with any of these or fastest available is chosen by default (BigFileTools::createDefault())

 use BigFileToolsBigFileTools;
 use BigFileToolsDriver;
 $bigFileTools = new BigFileTools(new DriverCurlDriver());
Wednesday, June 2, 2021
answered 5 Months ago

You can use linecache.getline:

linecache.getline(filename, lineno[, module_globals])

Get line lineno from file named filename. This function will never raise an exception — it will return '' on errors (the terminating newline character will be included for lines that are found).

import linecache

line = linecache.getline("foo.csv",24)

Or use the consume recipe from itertools to move the pointer:

import collections
from itertools import islice

def consume(iterator, n):
    "Advance the iterator n-steps ahead. If n is none, consume entirely."
    # Use functions that consume iterators at C speed.
    if n is None:
        # feed the entire iterator into a zero-length deque
        collections.deque(iterator, maxlen=0)
        # advance to the empty slice starting at position n
        next(islice(iterator, n, n), None)

with open("foo.csv") as f:
    line = next(f)
Wednesday, August 25, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :