简体   繁体   English

在php中读取一行

[英]read a single line in php

I have a file with 20 lines , with 3 numbers in each line... separated by spaces.. I just need to read 1 line at a time so that i can store those 3 number in an array... and use them in the code...the next time it should read the 2nd line and store in the array..and soon..how should i proceed .. i have tried fgets 我有一个20行的文件,每行3个数字...用空格隔开。.我只需要一次读取1行,这样我就可以将这3个数字存储在数组中...并在其中使用代码...下一次它将读取第二行并存储在数组中..很快..我应该如何进行..我尝试了fgets

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

and this as well... 还有这个...

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

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

<?php

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

this example for Read single line from a big text file .try this 此示例用于从大文本文件读取单行。

You can use fgets like this: 您可以使用以下fget:

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

// Close the handle
fclose($fh);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM