2017-07-26 45 views
-1

我想从目录中读取所有文件,但不是显示第一个字符,我想要显示某一行f.e.第4行。从目录读取文件并回显某一行

<?php 
$directory = "content/"; 
$dir = opendir($directory); 
while (($file = readdir($dir)) !== false) { 
    $filename = $directory . $file; 
    $type = filetype($filename); 
    if ($type == 'file') { 
    $contents = file_get_contents($filename); 
    $items = explode("|", $contents); 
    foreach ($items as $item) { 
     echo "$item[0]"; 
    } 
    } 
} 
closedir($dir); 
?> 

谢谢!

+0

您的“线条”是否分为“$ items”?然后你可以在第四行回显'$ items [3]'。否则,使用'“\ n”'而不是'“|”'爆炸以获取线条。 – rickdenhaan

回答

-1

您能否显示您的文件结构? Basicaly你可以使用这样的事情:

$contents = file_get_contents($filename); 
$items = explode("\r\n", $contents); //explode file content 
foreach ($items as $item) { 
    echo $item[3]; //echo your line 
} 
+0

不幸的是我得到了所有文件所有行的第三列 - 而不是所有文件的第三行。 – METZGERR

1

可以使用file()功能。它将文件读入一个数组,其中每一行都将是一个数组成员,跳过空行可以通过标志参数FILE_SKIP_EMPTY_LINES

欲了解更多信息,请参阅docs

0
// `$items` is already an array 
$items = explode("|", $contents); 
// if you want first element of array just: 
echo $item[0]; 
// if you want fourth element of array just: 
echo $item[3]; 
// without `foreach`