谁能告诉我如何跳过文件的第一行和最后一行。我编写了两个函数,它们返回文件的第一行和最后一行,但是我需要在标题和尾部之间添加一行!
function get_file_header($file) {
return fgets($file);
}
function get_file_tail($file){
while (( $line = fgets($file) )){
$fin = $line;
}
return $fin;
}
您可以先使用读取数组中的行file
,然后使用进行删除array_slice
:
function get_file_tail($filepath){
// Read file as lines into an array
$lines = file($filepath);
// Remove first and last line
$lines = array_slice($lines, 1, count($lines)-2);
// Convert to string (if array is not useful for you) and return it
return implode(PHP_EOL, $lines);
}
示例调用:
echo get_file_tail("http://websitetips.com/articles/copy/lorem/ipsum.txt");
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句