在php中读取一行


read a single line in php

我有一个有20行的文件,每行有3个数字。。。用空格分隔。。我只需要一次读取一行,这样我就可以将这3个数字存储在一个数组中。。。并在代码中使用它们。。。下一次它应该读取第2行并存储在数组中。。很快。。我该怎么办。。我试过fgets

 $fh = fopen($argv[1], "r");
    while($i<=20)
    {
    $line = trim($fh);
    $str=explode($line);
    }

还有这个。。。

$i=1;
while($i<=20)
{
$lines = file($argv[1], FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
foreach ($lines as $l){}

使用fgetcsv()-不要忘记将分隔符设置为空格。

<?php
function ReadLineNumber($file, $number)
{
    $handle = fopen($file, "r");
    $i = 0;
    while (fgets($handle) && $i < $number - 1)
        $i++;
    return fgets($handle);
}
?>

这个例子是从一个大的文本文件中读取单行。试试这个

您可以像这样使用fgets:

$fh = fopen($argv[1], "r");
if ($fh) 
{
    while (($line = fgets($fh)) !== false) 
    {
        // process the line read.
    }
} 
else 
{
    // error
} 
// Close the handle
fclose($fh);