1

我想读取文件文本中的一行。EX line 5. 任何人都可以帮助我吗????

$fp=fopen("test.txt",r)or exit("khong tim thay file can mo");
while(!feof($fp)){
    echo fgets($fp);
}
fclose($fp);

感谢阅读

4

3 回答 3

3

您可以使用 fgets() 函数逐行读取文件:

<?php 
$handle = fopen("test.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        echo $line.'<br/>';
    }

    fclose($handle);
} else {
    // error opening the file.
} 
?>
于 2015-06-25T08:11:15.417 回答
0

只需在循环中放置一个增量计数器,仅在该计数器与您要求的行号匹配时才回显,然后您可以简单地跳出循环

$required = 5;

$line = 1;
$fp=fopen("test.txt",r)or exit("khong tim thay file can mo");
while(!feof($fp)){
    if ($line == $required) {
        echo fgets($fp);
        break;
    }
    ++$line;
}
fclose($fp);
于 2015-06-25T08:06:36.650 回答
0
$myFile = "text.txt";
$lines = file($myFile);//file in to an array
echo $lines[1]; //line 2

PHP - Reads entire file into an array

于 2015-06-25T08:13:44.430 回答