How can I skip the first and the last line of a file in PHP

Yahya TAJANY

Can anyone tell me please how I can skip the first and the last line a file. I wrote two functions that return the first and the last line of a file but I need a the lines between the header and the tail !

function get_file_header($file)  {
  return fgets($file);
}  


function get_file_tail($file){
  while (( $line = fgets($file) )){
    $fin = $line;
  }
  return $fin;
}
trincot

You could read the lines in an array first with file and then perform the removal with 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);
}

Example call:

echo get_file_tail("http://websitetips.com/articles/copy/lorem/ipsum.txt");

Este artigo é coletado da Internet.

Se houver alguma infração, entre em [email protected] Delete.

editar em
0

deixe-me dizer algumas palavras

0comentários
loginDepois de participar da revisão

Artigos relacionados

How to skip reading the first line of file?

How can i skip the last response which has been requested first?

How can I skip line with awk

Skip first and last line from a pipe delimited file with 26 columns and make it to dataframe using scala

How do I print the first line of a file after sorting without the last value in powershell?

Skip last 5 line in a file using python

How can I read first n and last n lines from a file?

How to make first line of text file as header and skip second line in spark scala

how can i view the first line of every file in my directory using python

how can I sort a field form an Endnote Export File format where the Line contains GRAZ in the address as first line?

How can I make a script that always executes first to execute last?

How can I properly center the first and last items in a horizontal RecyclerView

Regex: How to insert text before the first line and after the last line of file

How can I use to the stream extraction operator to read a file assuming that only the last line will have errors of not containing 3 values

Why does `inputs` skip the first line of the input file?

How can I make a substitution on the line of first occurrence of a match only?

How can I skip the first few rows of a xlsx source in the OpenRowset if the sheet name has a space in it (SSIS)?

How can I remove last 2 lines in text file in Java?

How can I skip a stage if the agent is offline?

How can I skip even/odd rows while reading a csv file?

How can I use text-align-last except when I have only one line of text?

How do i make python choose randomly one line after the first line of a file?

How do i make python choose randomly one line after the first line of a file?

How can I print 2 lines if the second line contains the same match as the first line?

How can I create a new "file type" in the command line?

How can i cast the .nth() line in a file as an integer?

How can I use each line of a file as an input switch in bash?

How can I use File.AppendAllText and add to it a new line?

php How can I escape line break characters in a POST variable

TOP lista

  1. 1

    R Shiny: use HTML em funções (como textInput, checkboxGroupInput)

  2. 2

    O Chromium e o Firefox exibem as cores de maneira diferente e não sei qual deles está fazendo certo

  3. 3

    Como assinar digitalmente um documento PDF com assinatura e texto visíveis usando Java

  4. 4

    R Folheto. Dados de pontos de grupo em células para resumir muitos pontos de dados

  5. 5

    Gerenciar recurso shake de Windows Aero com barra de título personalizado

  6. 6

    Como obter dados API adequados para o aplicativo angular?

  7. 7

    UITextView não está exibindo texto longo

  8. 8

    Por que meus intervalos de confiança de 95% da minha regressão multivariada estão sendo plotados como uma linha de loess?

  9. 9

    Acessando relatório de campanhas na AdMob usando a API do Adsense

  10. 10

    Usando o plug-in Platform.js do Google

  11. 11

    Como posso modificar esse algoritmo de linha de visada para aceitar raios que passam pelos cantos?

  12. 12

    Dependência circular de diálogo personalizado

  13. 13

    Coloque uma caixa de texto HTML em uma imagem em uma posição fixa para site para desktop e celular

  14. 14

    iOS: como adicionar sombra projetada e sombra de traço no UIView?

  15. 15

    Como usar a caixa de diálogo de seleção de nomes com VBA para enviar e-mail para mais de um destinatário?

  16. 16

    Tabela CSS: barra de rolagem para a primeira coluna e largura automática para a coluna restante

  17. 17

    How to create dynamic navigation menu select from database using Codeigniter?

  18. 18

    Converter valores de linha SQL em colunas

  19. 19

    ChartJS, várias linhas no rótulo do gráfico de barras

  20. 20

    用@StyleableRes注释的getStyledAttributes。禁止警告

  21. 21

    não é possível adicionar dependência para com.google.android.gms.tasks.OnSuccessListener

quentelabel

Arquivo